#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
	int N;
	cin >> N;
	vector<queue<int>> Q(N + 1);
	for (int i = 0; i < N; i++){
		for (int j = 0; j < N - 1; j++){
			int A;
			cin >> A;
			A--;
			Q[i].push(A);
		}
		Q[i].push(N);
	}
	Q[N].push(N);
	int ans = 0;
	int cnt = N * (N - 1);
	bool ok = true;
	vector<int> upd;
	for (int i = 0; i < N; i++){
		upd.push_back(i);
	}
	while (cnt > 0){
		ans++;
		vector<int> upd2;	
		for (int i : upd){
			if (Q[Q[i].front()].front() == i && Q[i].front() > i){
				upd2.push_back(i);
				upd2.push_back(Q[i].front());
			}
		}
		for (int i : upd2){
			Q[i].pop();
			cnt--;
		}
		if (upd2.empty()){
			ok = false;
			break;
		}
	}
	if (!ok){
		cout << -1 << endl;
	} else {
		cout << ans << endl;
	}
}
#include<iostream>
#include<vector>
using namespace std;

int main(){
    int n;
    cin>>n;
    vector<vector<pair<int,int>>> v(n,vector<pair<int,int>>());
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        for(int j=0;j<a;j++){
            int x,y;
            cin>>x>>y;
            x--;
            v[i].push_back(make_pair(x,y));
        }
    }
    int ans=0;
    for(int i=0;i<(1<<n);i++){
        int c=0;
        vector<bool> f(n,false);
        for(int j=0;j<n;j++){
            if(i&(1<<j)){
                f[j]=true;
                c++;
            }
        }
        bool ok=true;
        for(int j=0;j<n;j++){
            if(f[j]){
                for(auto p : v[j]){
                    if(p.second==1 && !f[p.first]) ok=false;
                    if(p.second==0 && f[p.first]) ok=false;
                }
            }
        }
        if(ok) ans=max(ans,c);
    }
    cout<<ans<<endl;
}
