#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define debug1(a) cout<<a<<endl;
#define debug2(a,b) cout<<a<<' '<<b<<endl;
#define debug3(a,b,c) cout<<a<' '<<b<<' '<<c<<endl;
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,a,b)for(int i=a;i<b;i++)
#define repre(i,a,b)for(int i=a;i<=b;i++)
#define pi pair<int,int>
#define pii pair<int,pi>
#define all(v) v.begin(),v.end()
#define pq priority_queue
#define mpq priority_queue<int,vector<int>,greater<int> >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> orderedSet;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> orderedMSet;
//*p.find_by_order(index) return value at index
//p.order_of_key(key) return index
const int maxn=1009;
int dp[maxn];
int arr[maxn][maxn];
int visited[maxn];
int n;
int check(int day)
{
//cout<<day<<endl;
repre(i,0,n)dp[i]=1;
int match=0;
int c=0;
while(day--)
{
repre(i,0,n)visited[i]=0;
match=0;
repre(i,1,n)
{
if(dp[i]==n || visited[i]==1)continue;
int oppo=arr[i][dp[i]];
if(visited[oppo] || dp[oppo]==n)continue;
int uskaoppo=arr[oppo][dp[oppo]];
if(uskaoppo==i)
{
visited[i]=1;
visited[oppo]=1;
dp[i]++;
dp[oppo]++;
if(dp[i]==n)c++;
if(dp[oppo]==n)c++;
match=1;
}
}
if(c==n)break;
if(match==0)return -1;
}
if(c==n)return 1;
return 0;
}
int32_t main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fastio
cin>>n;
repre(i,1,n)
{
repre(j,1,n-1)
{
cin>>arr[i][j];
}
}
int low=1;
int high=(n*(n-1))/2;
int temp;
int flag=0;
while(low<high)
{
int mid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tvcHBvXSsrOwogICAgICAgIGlmKGRwW2ldPT1uKWMrKzsKICAgICAgICBpZihkcFtvcHBvXT09biljKys7CiAgICAgICAgbWF0Y2g9MTsKICAgICAgfQogICAgfQogICAgaWYoYz09bilicmVhazsKICAgIGlmKG1hdGNoPT0wKXJldHVybiAtMTsKICB9CiAgaWYoYz09bilyZXR1cm4gMTsKICByZXR1cm4gMDsKfQppbnQzMl90IG1haW4oKQp7CiAgLy9mcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAvL2ZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiAgZmFzdGlvCiAgY2luPj5uOwogIHJlcHJlKGksMSxuKQogIHsKICAgIHJlcHJlKGosMSxuLTEpCiAgICB7CiAgICAgIGNpbj4+YXJyW2ldW2pdOwogICAgfQogIH0KICBpbnQgbG93PTE7CiAgaW50IGhpZ2g9KG4qKG4tMSkpLzI7CiAgaW50IHRlbXA7CiAgaW50IGZsYWc9MDsKICB3aGlsZShsb3c8aGlnaCkKICB7CiAgICBpbnQgbWlkPShsb3craGlnaCkvMjsKICAgIHRlbXA9Y2hlY2sobWlkKTsKICAgIGlmKHRlbXA9PS0xKQogICAgewogICAgICBmbGFnPTE7CiAgICAgIGJyZWFrOwogICAgfQogICAgaWYodGVtcD09MSloaWdoPW1pZDsKICAgIGVsc2UgbG93PW1pZCsxOwogIH0KICBpZihmbGFnPT0xKWNvdXQ8PCItMSI8PCdcbic7CiAgZWxzZQogIGNvdXQ8PGxvdzw8J1xuJzsKICByZXR1cm4gMDsKfQo=