#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')w=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9') 
		s=s*10+ch-'0',ch=getchar();
	return s*w;
}
#define maxn 1009
bool vis[maxn];
int a[maxn][maxn],dd[maxn];
signed main()
{
	int n,cnt=0;
	scanf("%lld",&n);
	for (int i=1;i<=n;i++)
	{
		dd[i]=1;
		for (int j=1;j<n;j++)
			a[i][j]=read();
	}
	int ans=0;
	bool flag=1;
	while (flag)
	{
		ans++;
		bool vis[maxn]={};
		flag=0;
		for (int i=1;i<=n;i++)
		{
			int temp=a[i][dd[i]];
			if (dd[i]==n||vis[i]||vis[temp]||a[temp][dd[temp]]!=i||dd[temp]==n) continue;
			flag=1;
			vis[i]=vis[temp]=1;
			dd[i]++;
			dd[temp]++;
		}
	}
	for (int i=1;i<=n;i++)
		if (dd[i]!=n)
		{
			puts("-1");
			return 0;
		}
	printf("%lld\n",ans-1);
	return 0;
}
