#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;

typedef long long ll;
const int mod = 1e9+7;
const int MAXN = 2e5 + 7;

int match[1007][1007];
int ans[1007];
queue<int> q[1007];
int main(int argc, char const *argv[])
{
	int n;
	scanf("%d", &n);
	rep(i, 1, n){
		rep(j, 1, n-1){
			scanf("%d", &match[i][j]);
			q[i].push(match[i][j]);
		}
	}
	while(1){
		bool flag = 0;
		int top;
		rep(i, 1, n){
			if(q[i].empty())	continue;
			top = q[i].front();
			if(!q[top].empty() && q[top].front() == i){
				flag = 1;
				q[top].pop();
				q[i].pop();
				ans[top] = ans[i] = max(ans[i], ans[top]) + 1;
			}
		}
		int cnt = 0;
		rep(i, 1, n)	cnt += q[i].empty();
		if(cnt == n)	break;
		if(flag == 0){
			printf("-1\n");
			return 0;
		}
	}
	printf("%d\n", *max_element(ans+1, ans+1+n));
	return 0;
}