#include <bits/stdc++.h>
using namespace std;
typedef pair<double, double> P;
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 x;
			cin>>x;
			q[i].push(x-1);
		}
		q[i].push(n);
	}
	q[n].push(n);
	int ans=0;
	int cnt=n*(n-1);
	int f=1;
	vector<int>update;
	for (int i=0;i<n;i++){
		update.push_back(i);
	}
	while (cnt > 0){
		ans++;
		vector<int>update2;	
		for (int i : update){
			if (q[q[i].front()].front()==i && q[i].front()>i){
				update2.push_back(i);
				update2.push_back(q[i].front());
			}
		}
		for (int i:update2){
			q[i].pop();
			cnt--;
		}
		if (update2.empty()){
			f=0;
			break;
		}
	}
	if(f==0) cout<<-1<<endl;
	else cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}, dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<ll> a(n + 5);
	rep(i, n) cin >> a[i];
	if (n == 2) {
		cout << max(a[0], a[1]) << endl;
		return 0;
	}
	vvl dp(n + 7, vl(3, -INF));
	rep(i, 3) dp[i][i] = 0;
	rep(i, n) {
		rep(j, 3) {
			if (dp[i][j] == -INF) continue;
			dp[i + 2][j] = max(dp[i + 2][j], dp[i][j] + a[i]);
			if (j < 2) dp[i + 3][j + 1] = max(dp[i + 3][j + 1], dp[i][j] + a[i]);
			if (j < 1) dp[i + 4][j + 2] = max(dp[i + 4][j + 2], dp[i][j] + a[i]);
		}
	}
	ll ans = -INF;
	if (n % 2)
		rep(i, 3) ans = max(ans, dp[n - 1 + i][i]);
	else
		rep(i, 2) ans = max(ans, dp[n + i][i]);
	cout << ans << endl;
	return 0;
}
