#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
const int N = 2e5 + 10, OO = 0x3f3f3f3f, mod = 1e9 + 7;

#define PI acos(-1)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define loop(i, n) for (int i = 0; i < int(n); i++)
#define rloop(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define xloop(i, a, b) for (int i = int(a); i <= int(b); i++)
#define ALL(v) ((v).begin()), ((v).end())
#define SZ(v) ((int)((v).size()))
//int dx[] = {1, 0, -1, 0};
//int dy[] = {0, 1, 0, -1}; //4 Direction
//int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
//int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; //8 direction
int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

//-- "كلٌّ من يسعى لابد يصل .. كلٌّ من يسعى مضمون له الوصول" --//



int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	
	string s;
	cin >> s;
	int len = s.size();
	if(len < 26){
		int vis[26] = {};
		for(int i = 0 ; i < len ; ++i)
			vis[s[i]-'a']++;
		for(int i = 0 ; i < 26 ; ++i)
			if(!vis[i])
				return cout << s << char(i+'a') << "\n" , 0;
	}
	else{
		string temp = "";
		for(int i = 25 ; i >= 0 ; --i)
			temp += char(i + 'a');
		if(s == temp)
			cout << "-1\n";
		else{
			set < char > st;
			for(int i = 25 ; i >= 0 ; --i){
				st.insert(s[i]);
				if(s[i-1] < s[i]){ // abc
					char add = *st.upper_bound(s[i-1]);
					cout << s.substr(0 , i-1) << add << "\n";
					break;
				}
			}
		}
	}
	
	
    return 0;
}



#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;
const int Max_N=1010;

int A[Max_N][Max_N];


int main(){
	int N;
	cin >> N;
	rep(i,N){
		rep(j,N-1){
			int a;
			cin >> a;
			a--;
			A[i][j]=a;
		}
	}
	vector<int> op(N);
	int ans=0, sum=N;
	queue<int> q1, q2;	
	rep(a,N){
		int n=A[a][0], m=op[n];
		if(n<a) continue;
		if(A[n][m]==a) q1.push(a);
	}
	while(sum){
		ans++;
		if(q1.empty()){
			cout << -1 << endl;
			return 0;
		}
		while(!q1.empty()){
			int a=q1.front(); q1.pop();
			int n=A[a][op[a]], m=op[n];
			q2.push(a), q2.push(n);
			op[a]++; op[n]++;
			if(op[a]==N-1) op[a]=-1, sum--;
			if(op[n]==N-1) op[n]=-1, sum--;
		}
		vector<bool> pushed(N);
		while(!q2.empty()){
			int a=q2.front(); q2.pop();
			if(op[a]==-1) continue;
			int n=A[a][op[a]], m=op[n];
			if(pushed[n]) continue;
			if(A[n][m]==a) q1.push(a), pushed[a]=true;
		}
	}
	cout << ans << endl;

	return 0;
}