#include <iostream>

using namespace std;

int main(){
    int N;
    cin >> N;

    int A[N][N-1];
    for(int i=0; i<N; i++){
        for(int j=0; j<N-1; j++){
            int t;
            cin >> t;
            t--;
            A[i][j] = t;
        }
    }

    bool used[N];
    int next[N];
    fill(used, used+N, false);
    fill(next, next+N, 0);

    int cnt = 0;
    while(true){
        bool fl = false;
        for(int i=0; i<N; i++){
            if(next[i] == N-1) continue;
            int p = A[i][next[i]];
            if(used[i]) continue;
            if(used[A[i][p]]) continue;

            if(A[p][next[p]] == i){
                fl = true;
                used[i] = true;
                used[p] = true;
                next[i]++;
                next[p]++;
            }
        }
        if(!fl) break;
        fill(used, used+N, false);
        cnt++;
    }

    int f = true;
    for(int i=0; i<N; i++){
        if(next[i] != N-1) f = false;
    }

    if(f) cout << cnt << endl;
    else cout << -1 << endl;

    return 0;
}
