#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n <= 5)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << 1 << " " << 2 << endl;
        for (int i = 3; i <= n; i += 2)
        {
            cout << 1 << " " << i << endl;
        }
        for (int i = 4; i <= n; i += 2)
        {
            cout << 2 << " "<< i << endl;
        }
    }
    for (int i = 2; i <= n; i ++)
    {
        cout << 1 << " " << i << endl;
    }
    return 0;
}


#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <numeric>
#include <cmath>
#include <queue>
#include <sstream>
#include <unordered_map>
using namespace std;
map<int, int> m;

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 0; i < 9; i++)
    {
        int x;
        cin >> x;
        if (x > i + 1)
        {
            m[i + 1] = x;
        }
    }
    bool fl = false;
    for (int i = 0; i < n; i++)
    {
        if (m[s[i] - '0'])
        {
            fl = true;
            s[i] = char(m[s[i] - '0'] + '0');
        }
        else if (fl)
        {
            break;
        }
    }
    cout << s << endl;
}


#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <numeric>
#include <cmath>
#include <queue>
#include <sstream>
#include <unordered_map>
using namespace std;
map<int, int> m;

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 0; i < 9; i++)
    {
        int x;
        cin >> x;
        if (x >= i + 1)
        {
            m[i + 1] = x;
        }
    }
    bool fl = false;
    for (int i = 0; i < n; i++)
    {
        if (m[s[i] - '0'])
        {
            fl = true;
            s[i] = char(m[s[i] - '0'] + '0');
        }
        else if (fl)
        {
            break;
        }
    }
    cout << s << endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>

using namespace std;

int main() {
	unsigned n;
	cin >> n;
	vector<int> nmb(n);
	cin.get();
	for (int i = 0; i < n; i++) {
		nmb[i] = cin.get() - 48;
	}
	cin.get();
	vector<int> fx(9);
	for (int i = 0; i < 9; i++) {
		cin >> fx[i];
	}
	bool chSt = 0;
	for (int i = 0; i < n; i++) {
		if (nmb[i] <= fx[nmb[i] - 1]) {
			nmb[i] = fx[nmb[i] - 1];
			chSt = true;
		}
		else if (chSt) break;
	}
	for (int i = 0; i < n; i++) {
		cout << nmb[i];
	}
	cout << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>

using namespace std;

int main() {
	unsigned n;
	cin >> n;
	vector<int> nmb(n);
	cin.get();
	for (int i = 0; i < n; i++) {
		nmb[i] = cin.get() - 48;
	}
	cin.get();
	vector<int> fx(9);
	for (int i = 0; i < 9; i++) {
		cin >> fx[i];
	}
	bool chSt = 0;
	for (int i = 0; i < n; i++) {
		if (nmb[i] < fx[nmb[i] - 1]) {
			nmb[i] = fx[nmb[i] - 1];
			chSt = true;
		}
		else if (chSt) break;
	}
	for (int i = 0; i < n; i++) {
		cout << nmb[i];
	}
	cout << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>

using namespace std;

int main() {
	unsigned n;
	cin >> n;
	vector<int> nmb(n);
	cin.get();
	for (int i = 0; i < n; i++) {
		nmb[i] = cin.get() - 48;
	}
	cin.get();
	vector<int> fx(9);
	for (int i = 0; i < 9; i++) {
		cin >> fx[i];
	}
	bool chSt = 0;
	for (int i = 0; i < n; i++) {
		if (nmb[i] < fx[nmb[i] - 1]) {
			nmb[i] = fx[nmb[i] - 1];
			chSt = true;
		}
		else if (chSt && nmb[i] != fx[nmb[i] - 1]) break;
	}
	for (int i = 0; i < n; i++) {
		cout << nmb[i];
	}
	cout << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	for (int i = 0; i < num.length(); i++) {
		while (i < num.length() && num[i] - '0' < fun[num[i] - '0' - 1]) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
		}
		if (num > ans) {
			ans = num;
			break;
		}
		num = number;
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	for (int i = 0; i < num.length(); i++) {
		while (i < num.length() && (fun[num[i] - '0' - 1] > num[i] - '0')) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
		}
		if (num > ans) {
			ans = num;
			break;
		}
		num = number;
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	bool pupa = 0;
	for (int i = 0; i < num.length(); !pupa ? i++ : i += 0) {
		while (i < num.length() && (fun[num[i] - '0' - 1] > num[i] - '0')) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
			pupa = 1;
		}
		if (num > ans) {
			ans = num;
			break;
		}
		num = number;
	}
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	string number;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	int ans = stoi(number);
	string num = number;
	for (int i = 0; i < num.length(); i++) {
		while (i < num.length() && num[i] - '0' < fun[num[i] - '0' - 1]) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
		}
		int tmp = stoi(num);
		if (tmp > ans) {
			ans = tmp;
		}
		num = number;
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

bool operator>(string a, string b) {
	for (int i = 0; i < a.length(); i++) {
		if ((a[i] - '0') < (b[i] - '0')) {
			return false;
		}
	}
	return true;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	for (int i = 0; i < num.length(); i++) {
		while (i < num.length() && num[i] - '0' < fun[num[i] - '0' - 1]) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
		}
		if (num > ans) {
			ans = num;
		}
		num = number;
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	for (int i = 0; i < num.length(); i++) {
		while (i < num.length() && num[i] - '0' < fun[num[i] - '0' - 1]) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
		}
		if (num > ans) {
			ans = num;
			break;
		}
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	bool pupa = 0;
	for (int i = 0; i < num.length(); !pupa ? i++ : i += 0) {
		pupa = 0;
		while (i < num.length() && (fun[num[i] - '0' - 1] > num[i] - '0')) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
			pupa = 1;
		}
		if (num > ans) {
			ans = num;
			break;
		}
		num = number;
	}
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <map>
#include <locale.h>
#include <cmath>
#include <bitset>

#define ll long long
#define M_PI 3.14159265358979323846
#define all(x) x.begin(), x.end()

using namespace std;

string number;
string num;
string ans;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	cin >> number;
	vector<int> fun(9);
	for (int i = 0; i < fun.size(); i++) {
		cin >> fun[i];
	}
	num = number;
	ans = number;
	bool pupa = 0;
	for (int i = 0; i < num.length(); !pupa ? i++ : i += 0) {
		pupa = 0;
		while (i < num.length() && (fun[num[i] - '0' - 1] >= num[i] - '0')) {
			num[i] = char(fun[num[i] - '0' - 1] + '0');
			i++;
			pupa = 1;
		}
		if (num > ans) {
			ans = num;
			break;
		}
		num = number;
	}
	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
	int n;
	cin >> n;
	string number;
	cin >> number;

	vector<int> f(9);
	for (int i = 0; i < 9; i++) {
		cin >> f[i];
	}

	bool started = false;

	for (int i = 0; i < n; i++) {
		int curNum = number[i] - '0';		//char to int gives ascii so -'0'

		if (curNum <= f[curNum - 1]) {		//number[i] can't be 0
			cout << f[curNum - 1];
			started = true;
			continue;
		}
		if (!started) {
			cout << number[i];
		}
		else {
			while (i < n) {
				cout << number[i];
				i++;
			}
		}
	}
	cout << endl;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
	int n;
	cin >> n;
	string number;
	cin >> number;

	vector<int> f(9);
	for (int i = 0; i < 9; i++) {
		cin >> f[i];
	}

	bool started = false;

	for (int i = 0; i < n; i++) {
		int curNum = number[i] - '0';		//char to int gives ascii so -'0'

		if (curNum < f[curNum - 1]) {		//We start when we get a strictly bigger number
			cout << f[curNum - 1];
			started = true;
			continue;
		}
		if (!started || curNum == f[curNum-1]) {
			cout << number[i];
		}
		else {
			while (i < n) {				//because transformations should be contiguous
				cout << number[i];
				i++;
			}
		}
	}
	cout << endl;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>=i) mp[i]=tmp[i];
    }
    int ans=0;
    int flag=0;
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            flag++;
            book[i]=mp[book[i]-'0']+'0';
        }else if(!mp[book[i]-'0'])
        {
            ans++;
        }
        if(flag && ans) break;
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>=i) mp[i]=tmp[i];
    }
    int ans=0;
    int flag=0;
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            flag++;
            ans=0;
            book[i]=mp[book[i]-'0']+'0';
        }else if(!mp[book[i]-'0'])
        {
            ans++;
        }
        if(flag && ans) break;
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>=i) mp[i]=tmp[i];
    }
    int ans=0;
    int flag=0;
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            if(flag==0) ans=0;
            flag++;
            book[i]=mp[book[i]-'0']+'0';
        }else if(!mp[book[i]-'0'])
        {
            ans++;
        }
        if(flag && ans) break;
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>i)
        mp[i]=tmp[i];
    }
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            book[i]=mp[book[i]-'0']+'0';
        }
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>=i)
        mp[i]=tmp[i];
    }
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            book[i]=mp[book[i]-'0']+'0';
            continue;
        }
        break;
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>i) mp[i]=tmp[i];
        if(tmp[i]==i) mp[i]=10;//标记相等的数字
    }
    int ans=0;
    int flag=0;
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            if(flag==0) ans=0;
            if(mp[book[i]-'0']!=10)
            {
                flag++;          //相等不记录
                book[i]=mp[book[i]-'0']+'0';
            }
        }else if(!mp[book[i]-'0'])
        {
            ans++;
        }
        if(flag && ans) break;
    }
    puts(book);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<char,int> ma;
int main(){
    string s;
    int n;
    cin>>n>>s;
//                                cout<<n<<endl<<s<<endl;
    for(int i=1;i<10;++i){
        int a;
        cin>>a;
        ma[(char)(i+48)]=a;
      //  cout<<(char)(i+48)<<"   "<<ma[i+48]<<endl;
    }
    int len=s.length();
    for(int i=0;i<len;++i){
        if(ma[s[i]]>(s[i]-48)){
            s[i]=(char)(ma[s[i]]+48);
            ++i;
            while(ma[s[i]]>(s[i]-48) && i<len){
                s[i]=(char)(ma[s[i]]+48);
                i++;
            }
            break;
        }
    }
    cout<<s;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<char,int> ma;
int main(){
    string s;
    int n;
    cin>>n>>s;
//                                cout<<n<<endl<<s<<endl;
    for(int i=1;i<10;++i){
        int a;
        cin>>a;
        ma[(char)(i+48)]=a;
      //  cout<<(char)(i+48)<<"   "<<ma[i+48]<<endl;
    }
    int len=s.length();
    for(int i=0;i<len;++i){
        if(ma[s[i]]>(s[i]-48)){
            s[i]=(char)(ma[s[i]]+48);
            ++i;
            while(ma[s[i]]>=(s[i]-48) && i<len){
                s[i]=(char)(ma[s[i]]+48);
                i++;
            }
            break;
        }
    }
    cout<<s;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<char,int> ma;
int main(){
    string s;
    int n;
    cin>>n>>s;
//                                cout<<n<<endl<<s<<endl;
    for(int i=1;i<10;++i){
        int a;
        cin>>a;
        ma[(char)(i+48)]=a;
      //  cout<<(char)(i+48)<<"   "<<ma[i+48]<<endl;
    }
    int len=s.length();
    for(int i=0;i<len;++i){
        if(ma[s[i]]>(s[i]-48)){
            s[i]=ma[s[i]]+48;
            ++i;
            while(ma[s[i]]>(s[i]-48) && i<len){
                s[i]=ma[s[i]]+48;
                i++;
            }
            break;
        }
    }
    cout<<s;
    return 0;
}


#include<iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200010];
	int F[20];
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i <= 9; i++)
		cin >> F[i];
	for (int i = 0; i < n; i++)
		if (a[i] - '0' < F[a[i] - '0'])
			a[i] = F[a[i] - '0'] + '0';
		else
			break;
	for (int i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200010];
	int F[20];
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i <= 9; i++)
		cin >> F[i];
	for (int i = 0; i < n; i++)
		if (a[i] - '0' <= F[a[i] - '0'])
			a[i] = F[a[i] - '0'] + '0';
		else
			break;
	for (int i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200010];
	int F[20];
	int flag;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i <= 9; i++)
		cin >> F[i];
	for (int i = 0; i < n; i++)
		if (a[i] - '0' <= F[a[i] - '0'])
		{
			a[i] = F[a[i] - '0'] + '0';
			flag = 1;
		}
		else if(flag==1)
			break;
	for (int i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200010];
	int F[20];
	int flag=0;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i <= 9; i++)
		cin >> F[i];
	for (int i = 0; i < n; i++)
		if (a[i] - '0' < F[a[i] - '0'])
		{
			a[i] = F[a[i] - '0'] + '0';
			flag = 1;
		}
		else if (a[i] - '0' == F[a[i] - '0'])
			continue;
		else if(flag==1)
			break;
	for (int i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200010];
	int F[20];
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i <= 9; i++)
		cin >> F[i];
	for (int i = 0; i < n; i++)
		if (a[i] - '0' < F[a[i] - '0'])
			a[i] = F[a[i] - '0'] + '0';
	for (int i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>

#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
const int  Inf = 2e9;
ll LINF = (ll)4e18;
using namespace std;
#define M_PI       3.14159265358979323846   // pi

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	vector <int> a(n, 0);
	for (int i = 0; i < n; i++) {
		char tmp;
		cin >> tmp;
		a[i] = tmp - '0';
	}
	vector <int> b(10, 0);
	for (int i = 1; i <= 9; i++) {
		cin >> b[i];
	}
	bool flag = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] <= b[a[i]]) {
			flag = 1;
			a[i] = b[a[i]];
		}
		else if (flag) {
			break;
		}
	}
	for (auto &x : a) {
		cout << x;
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>

#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
const int  Inf = 2e9;
ll LINF = (ll)4e18;
using namespace std;
#define M_PI       3.14159265358979323846   // pi

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	vector <int> a(n, 0);
	for (int i = 0; i < n; i++) {
		char tmp;
		cin >> tmp;
		a[i] = tmp - '0';
	}
	vector <int> b(10, 0);
	for (int i = 1; i <= 9; i++) {
		cin >> b[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] <= b[a[i]]) a[i] = b[a[i]];
	}
	for (auto &x : a) {
		cout << x;
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>

#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
const int  Inf = 2e9;
ll LINF = (ll)4e18;
using namespace std;
#define M_PI       3.14159265358979323846   // pi

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	vector <int> a(n, 0);
	for (int i = 0; i < n; i++) {
		char tmp;
		cin >> tmp;
		a[i] = tmp - '0';
	}
	vector <int> b(10, 0);
	for (int i = 1; i <= 9; i++) {
		cin >> b[i];
	}
	bool flag = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] < b[a[i]]) {
			flag = 1;
			a[i] = b[a[i]];
		}
		else if (flag) {
			break;
		}
	}
	for (auto &x : a) {
		cout << x;
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>

#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
const int  Inf = 2e9;
ll LINF = (ll)4e18;
using namespace std;
#define M_PI       3.14159265358979323846   // pi

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	vector <int> a(n, 0);
	for (int i = 0; i < n; i++) {
		char tmp;
		cin >> tmp;
		a[i] = tmp - '0';
	}
	vector <int> b(10, 0);
	for (int i = 1; i <= 9; i++) {
		cin >> b[i];
	}
	bool flag = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == b[a[i]]) {
			continue;
		}
		if (a[i] < b[a[i]]) {
			flag = 1;
			a[i] = b[a[i]];
		}
		else if (flag) {
			break;
		}
	}
	for (auto &x : a) {
		cout << x;
	}

	return 0;
}


#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main() {
	int n;
	string a;
	vector<int> f(9);

	cin >> n >> a;

	for (size_t i = 0; i < 9; ++i)
		cin >> f[i];

	for (size_t num = 0; num < n; ++num) {
		if (f[a[num] - '0' - 1] > a[num] - '0') {
			while (num < n && f[a[num] - '0' - 1] >= a[num] - '0') {
				a[num] = f[a[num] - '0' - 1] + '0';
				++num;
			}
			break;
		}
	}
	cout << a;

#ifdef _DEBUG
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n, a;
	vector<int> f(9);

	cin >> n >> a;

	for (size_t i = 0; i < 9; ++i)
		cin >> f[i];

	vector<int> numbers(n);
	for (size_t num = n; num > 0; --num) {
		numbers[num - 1] = a % 10;
		a /= 10;
	}

	for (size_t num = 0; num < n; ++num) {
		if (f[numbers[num] - 1] > numbers[num]) {
			while (num < n && f[numbers[num] - 1] > numbers[num]) {
				numbers[num] = f[numbers[num] - 1];
				++num;
			}
			break;
		}
	}

	for (const auto& num : numbers)
		cout << num;

#ifdef _DEBUG
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n, a;
	vector<int> f(9);

	cin >> n >> a;

	for (size_t i = 0; i < 9; ++i)
		cin >> f[i];

	vector<int> numbers(n);
	for (size_t num = n; num > 0; --num) {
		numbers[num - 1] = a % 10;
		a /= 10;
	}

	for (size_t num = 0; num < n; ++num) {
		if (f[numbers[num] - 1] > numbers[num]) {
			while (num < n && f[numbers[num] - 1] >= numbers[num]) {
				numbers[num] = f[numbers[num] - 1];
				++num;
			}
			break;
		}
	}

	for (const auto& num : numbers)
		cout << num;

#ifdef _DEBUG
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n, a;
	vector<int> f(9);

	cin >> n >> a;

	for (size_t i = 0; i < 9; ++i)
		cin >> f[i];

	vector<int> numbers(n);
	for (size_t num = n; num > 0; --num) {
		numbers[num - 1] = a % 10;
		a /= 10;
	}

	for (size_t num = 0; num < n; ++num) {
		if (f[numbers[num] - 1] >= numbers[num]) {
			while (num < n && f[numbers[num] - 1] >= numbers[num]) {
				numbers[num] = f[numbers[num] - 1];
				++num;
			}
			break;
		}
	}

	for (const auto& num : numbers)
		cout << num;

#ifdef _DEBUG
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int f[10];
string s;

int main()
{
	int n;
	cin >> n;
	cin >> s;
	for(int i = 1; i <= 9; i++)
		cin >> f[i];
	vector<int> diff;
	for(int i = 0; i < n; i++)
		diff.push_back(f[s[i] - '0'] - (s[i] - '0'));
	for(int i = 0; i < n; i++)
		if(diff[i] > 0)
		{
			while(i < n && diff[i] >= 0)
			{
				s[i] = char(f[s[i] - '0'] + '0');
				i++;
			}
			break;
		}
	cout << s << endl;
}


#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main() {
#ifdef _DEBUG
	FILE *stream;
	freopen_s(&stream, "input.txt", "r", stdin);
	freopen_s(&stream, "output.txt", "w", stdout);
#endif
	int n;
	int a;
	vector<int> f(9);

	cin >> n >> a;

	for (size_t i = 0; i < 9; ++i)
		cin >> f[i];

	vector<unsigned short int> numbers(n);
	for (int num = n - 1; num >= 0; --num) {
		numbers[num] = a % 10;
		a /= 10;
	}

	for (size_t num = 0; num < n; ++num) {
		if (f[numbers[num] - 1] > numbers[num]) {
			while (num < n && f[numbers[num] - 1] >= numbers[num]) {
				numbers[num] = f[numbers[num] - 1];
				++num;
			}
			break;
		}
	}

	for (size_t num = 0; num < n; ++num)
		cout << numbers[num];

	return 0;
}


#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>

using namespace std;
int main()
{

    int n;
    cin >> n;
    string a;
    cin >> a;
    vector<int>p(11);
    for (int i = 1; i < 10; i++) { cin >> p[i];
    //p[i] = max(i, p[i]);

    }
    bool f = 0,r=0;
    for (int i = 0; i < n; i++) {
        char d = a[i];
        int t = d - '0';
        if (t > p[i] || f) { cout << t; if (r)f = 1; }
        else { cout << p[t]; r = 1; }
    }

    return 0;

}


#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>

using namespace std;
int main()
{

    int n;
    cin >> n;
    string a;
    cin >> a;
    vector<int>p(11);
    for (int i = 1; i < 10; i++) { cin >> p[i];
    //p[i] = max(i, p[i]);

    }
    bool f = 0,r=0;
    for (int i = 0; i < n; i++) {
        char d = a[i];
        int t = d - '0';
        if (t > p[t] || f) { cout << t; if (r)f = 1; }
        else { cout << p[t]; r = 1; }
    }

    return 0;

}


#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>

using namespace std;
int main()
{

    int n;
    cin >> n;
    string a;
    cin >> a;
    vector<int>p(11);
    for (int i = 1; i < 10; i++) { cin >> p[i];
    //p[i] = max(i, p[i]);

    }
    bool f = 0,r=0;
    for (int i = 0; i < n; i++) {
        char d = a[i];
        int t = d - '0';
        if (t >= p[t] || f) { cout << t; if (r)f = 1; }
        else { cout << p[t]; r = 1; }

    }

    return 0;

}


#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>

using namespace std;
int main()
{

    int n;
    cin >> n;
    string a;
    cin >> a;
    vector<int>p(11);
    for (int i = 1; i < 10; i++) { cin >> p[i];
    //p[i] = max(i, p[i]);

    }

    for (int i = 0; i < n; i++) {
        char d = a[i];
        int t = d - '0';

        if (a[i]-'0' < p[a[i]-'0']) {
            int j = i;

            while (a[j] - '0' <= p[a[j]-'0']&&j<n) {
                a[j] = p[a[j]-'0']+'0' ;
                j++;
                if (j == n)break;

            }
            break;

        }
    }
    cout << a << endl;

    return 0;

}


#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>

using namespace std;
int main()
{

    int n;
    cin >> n;
    string a;
    cin >> a;
    vector<int>p(11);
    for (int i = 1; i < 10; i++) { cin >> p[i];
    p[i] = max(i, p[i]);

    }

    for (int i = 0; i < n; i++) {
        char d = a[i];
        int t = d - '0';
        cout << p[t];
    }

    return 0;

}


#include <map>
#include <iostream>

using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	int f = 1;
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] > x[i] - '0'&&f == 1)
		{
			cout << a[x[i] - '0' - 1];
			if (a[x[i] - '0' - 1] < x[i])
				f = 0;
		}
		else
			cout << x[i];

	return 0;
}


// ConsoleApplication9.cpp : Defines the entry point for the console application.
//

#include <map>
#include <iostream>
#include <utility>
using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	pair <char, int> p;

	p.first = 'a';
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] > x[i] - '0')
		{
			p.first = x[i];
			p.second = a[x[i] - '0' - 1];
			break;
		}
	int f = 1;
	for (int i = 0; i < n; i++)
	{
		if (x[i] == p.first &&f == 1)
		{
			cout << p.second;
			if (x[i + 1] != p.first)
				f = 0;
		}
		else
			cout << x[i];
	}

    return 0;
}


#include <map>
#include <iostream>

using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	int f = 1;
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] >= x[i] - '0'&&f == 1)
		{
			cout << a[x[i] - '0' - 1];
			if (a[x[i+1] - '0' - 1] < x[i+1]-'0')
				f = 0;
		}
		else
			cout << x[i];

	return 0;
}


#include <map>
#include <iostream>

using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	int f = 1;
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] > x[i] - '0'&&f == 1)
		{
			cout << a[x[i] - '0' - 1];
			if (a[x[i+1] - '0' - 1] < x[i+1]-'0')
				f = 0;
		}
		else
			cout << x[i];

	return 0;
}


// ConsoleApplication9.cpp : Defines the entry point for the console application.
//

#include <map>
#include <iostream>
using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] > x[i] - '0')
			cout << a[x[i] - '0' - 1];
		else
			cout << x[i];

    return 0;
}


#include <map>
#include <iostream>

using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	int f = 1;
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] >= x[i] - '0'&&f == 1)
		{
			cout << a[x[i] - '0' - 1];
			if (a[x[i] - '0' - 1] < x[i])
				f = 0;
		}
		else
			cout << x[i];

	return 0;
}


// ConsoleApplication9.cpp : Defines the entry point for the console application.
//

#include <map>
#include <iostream>
#include <utility>
using namespace std;

int main()
{
	int n;
	char x[200005];
	cin >> n >> x;
	int a[9];
	pair <char, int> p;

	p.first = 'a';
	for (int i = 0; i < 9; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		if (a[x[i] - '0' - 1] > x[i] - '0')
		{
			p.first = x[i];
			p.second = a[x[i] - '0' - 1];
			break;
		}
	for (int i = 0; i < n; i++)
		if (x[i] == p.first)
		{
			cout << p.second;

		}
		else
			cout << x[i];

    return 0;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

typedef long long ll;
typedef long double ld;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	ll n;
	std::cin >> n;
	std::string S;
	std::cin >> S;
	char A[10];
	for (ll i = 1; i < 10; ++i)
		std::cin >> A[i];
	A[0] = 0;
	ll i;
	for (i = 0; i < n; ++i)
	{
		if (S[i] < A[S[i] - '0'])
			break;
	}
	while (i < n && S[i] < A[S[i] - '0'])
	{
		S[i] = A[S[i] - '0'];
		++i;
	}
	std::cout << S;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

typedef long long ll;
typedef long double ld;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	ll n;
	std::cin >> n;
	std::string S;
	std::cin >> S;
	char A[9];
	for (ll i = 0; i < 9; ++i)
		std::cin >> A[i];
	bool trigger = false;
	ll i;
	for (i = 0; i < n; ++i)
	{
		if (S[i] < A[S[i] - '1'])
			break;
	}
	while (i < n && S[i] < A[S[i] - '1'])
	{
		S[i] = A[S[i] - '1'];
		++i;
	}
	std::cout << S;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

typedef long long ll;
typedef long double ld;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	ll n;
	std::cin >> n;
	std::string S;
	std::cin >> S;
	char A[9];
	for (ll i = 0; i < 9; ++i)
		std::cin >> A[i];
	ll i;
	for (i = 0; i < n; ++i)
	{
		if (S[i] != '0' && S[i] < A[S[i] - '1'])
			break;
	}
	while (S[i] == '0' || i < n && S[i] < A[S[i] - '1'])
	{
		S[i] = A[S[i] - '1'];
		++i;
	}
	std::cout << S;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

typedef long long ll;
typedef long double ld;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	ll n;
	std::cin >> n;
	std::string S;
	std::cin >> S;
	char A[9];
	for (ll i = 0; i < 9; ++i)
		std::cin >> A[i];
	bool trigger = false;
	char max;
	char Save;
	ll i;
	for (i = 0; i < n; ++i)
	{
		if (S[i] < A[S[i] - '1'])
		{
			max = A[S[i] - '1'];
			Save = S[i];
			trigger = true;
			break;
		}
	}
	for (; i < n; ++i)
		if (Save == S[i])
			S[i] = max;
	std::cout << S;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

typedef long long ll;
typedef long double ld;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	ll n;
	std::cin >> n;
	std::string S;
	std::cin >> S;
	char A[9];
	for (ll i = 0; i < 9; ++i)
		std::cin >> A[i];
	ll i;
	for (i = 0; i < n; ++i)
	{
		if (S[i] != '0' && S[i] < A[S[i] - '1'])
			break;
	}
	while (S[i] == '0' || i < n && S[i] <= A[S[i] - '1'])
	{
		S[i] = A[S[i] - '1'];
		++i;
	}
	std::cout << S;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
long long gcd(long long a, long long b)
{
     if (b == 0)
         return a;
    return gcd(b, a % b);
}
int main()
{
  long long n,b[10];
  string a;
  cin>>n;
  cin>>a;
  map<int,int>maps;
  set<int>aibe;

  for(int i=0;i<9;i++)
  {
    cin>>b[i];
  }
  for(int i=0;i<n;i++)
  {
    if(b[a[i]-1-48]>a[i]-48)
    a[i]=b[a[i]-1-48]+48;

  }
  cout<<a;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
long long gcd(long long a, long long b)
{
     if (b == 0)
         return a;
    return gcd(b, a % b);
}
int main()
{
  long long n,b[10];
  string a;
  cin>>n;
  cin>>a;
  map<int,int>maps;
  set<int>aibe;

  for(int i=0;i<9;i++)
  {
    cin>>b[i];
  }
  int jau=0;
  for(int i=0;i<n;i++)
  {
    if(b[a[i]-1-48]>a[i]-48)
    {
      //int kk=a[i]-1-48;
    a[i]=b[a[i]-1-48]+48;
   // b[kk]=0;
jau=1;
    }
    else if(jau==1)
    {
cout<<a;
return 0;
    }

  }
  cout<<a;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
long long gcd(long long a, long long b)
{
     if (b == 0)
         return a;
    return gcd(b, a % b);
}
int main()
{
  long long n,b[10];
  string a;
  cin>>n;
  cin>>a;
  map<int,int>maps;
  set<int>aibe;

  for(int i=0;i<9;i++)
  {
    cin>>b[i];
  }
  int jau=0;
  for(int i=0;i<n;i++)
  {
    if(b[a[i]-1-48]>=a[i]-48)
    {
      //int kk=a[i]-1-48;
    a[i]=b[a[i]-1-48]+48;
   // b[kk]=0;
jau=1;
    }
    else if(jau==1)
    {
cout<<a;
return 0;
    }

  }
  cout<<a;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
long long gcd(long long a, long long b)
{
     if (b == 0)
         return a;
    return gcd(b, a % b);
}
int main()
{
  long long n,b[10];
  string a;
  cin>>n;
  cin>>a;
  map<int,int>maps;
  set<int>aibe;

  for(int i=0;i<9;i++)
  {
    cin>>b[i];
  }
  int jau=0;
  for(int i=0;i<n;i++)
  {
    if(b[a[i]-1-48]>a[i]-48)
    {
      //int kk=a[i]-1-48;
    a[i]=b[a[i]-1-48]+48;
   // b[kk]=0;
jau=1;
    }
else if(b[a[i]-1-48]==a[i]-48)
    {
      if(jau==0)
      {
;
      }
      else
      {
      //int kk=a[i]-1-48;
    a[i]=b[a[i]-1-48]+48;
   // b[kk]=0;
jau=1;
      }
    }
    else if(jau==1)
    {
cout<<a;
return 0;
    }

  }
  cout<<a;

}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'])
		{
			nn[i] = a[number[i] - '0'] + '0';
		}
		else
		{
			nn[i] = number[i];
		}
	}
	cout << nn;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string a;
int book[10];

int main()
{
	int n;
	cin >> n;
	cin >> a;
	for (int i = 1; i <= 9; i++)
	{
		int c;
		cin >> c;
		book[i] = c;
	}
	int flag = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] - '0' < book[a[i] - '0'])
		{
			a[i] = book[a[i] - '0'] + '0';
			flag = 1;
		}
		else
		{
			if (flag)
				break;
		}
	}
	cout << a;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'])
		{
			nn[i] = a[number[i] - '0'] + '0';
			flag++;
		}
		else
		{
			if (flag == 1)
			{
				flag = 0;
				nn[i - 1] = number[i - 1];
			}
			if (flag)
				break;
			nn[i] = number[i];
		}
	}
	for (; i < n; i++)
	{
		nn[i] = number[i];
	}
	cout << nn;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'])
		{
			nn[i] = a[number[i] - '0'] + '0';
			if (i != n - 1)
			{
				i++;
				nn[i] = a[number[i] - '0'] + '0';
			}
			flag = 1;
		}
		else
		{
			if (flag)
				break;
			nn[i] = number[i];
		}
	}
	for (; i < n; i++)
	{
		nn[i] = number[i];
	}
	cout << nn;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'])
		{
			nn[i] = a[number[i] - '0'] + '0';
			flag = 1;
		}
		else
		{
			if (flag)
				break;
			nn[i] = number[i];
		}
	}
	for (; i < n; i++)
	{
		nn[i] = number[i];
	}
	cout << nn;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n - 1; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'] && (number[i] == number[i + 1]))
		{
			nn[i] = a[number[i] - '0'] + '0';
			nn[i + 1] = a[number[i + 1] - '0'] + '0';
			if (i == n - 2)
				flag = 1;
			i++;
		}
		else
		{
			nn[i] = number[i];
		}
	}
	if (flag)
		nn[n - 1] = a[number[n - 1] - '0'] + '0';
	else
		nn[i] = number[i];
	cout << nn;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string a;
int book[10];

int main()
{
	int n;
	cin >> n;
	cin >> a;
	for (int i = 1; i <= 9; i++)
	{
		int c;
		cin >> c;
		book[i] = c;
	}
	int flag = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] - '0' <= book[a[i] - '0'])
		{
			if (a[i] - '0' == book[a[i] - '0'] && flag == 0)
				continue;
			a[i] = book[a[i] - '0'] + '0';
			flag = 1;
		}
		else
		{
			if (flag)
				break;
		}
	}
	cout << a;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int a[10];
string number;
char nn[200010];

int main()
{
	int n, flag = 0;
	int i;
	cin >> n >> number;
	for (int i = 1; i <= 9; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n - 1; i++)
	{
		if (number[i] - '0' < a[number[i] - '0'] && (number[i] == number[i + 1]))
		{
			nn[i] = a[number[i] - '0'] + '0';
			if (i == n - 2)
				flag = 1;
		}
		else
		{
			nn[i] = number[i];
		}
	}
	if (flag)
		nn[n - 1] = a[number[n - 1] - '0'] + '0';
	cout << nn;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <limits>
#include <set>
using namespace std;

int main() {
    string number;
    int size;
    int hash[9];
    vector<int> num;
    cin >> size;
    cin >> number;
    for(int i = 0; i < size; i++){
        num.push_back(number[i]-48);
    }
    for(int i = 0; i < 10; i++){
        cin >> hash[i];
    }
    int first = -1;
    int second = -1;
    for(int i = 0; i < size; i++){
        if(num[i]<hash[num[i]-1]){
            if(first == -1){
                first = i;
            }
        }
        else{
            if(first != -1){
                second = i;
                break;
            }
        }
    }
    if(second == -1){
        second = size;
    }
    if(first == -1){
        cout << number;
    }
    else{
        for(int i = first; i < second; i++){
            num[i]= hash[num[i]-1];
        }
        for(int i = 0; i < size; i++){
            cout << num[i];
        }
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <limits>
#include <set>
using namespace std;

int main() {
    string number;
    int size;
    int hash[9];
    vector<int> num;
    cin >> size;
    cin >> number;
    for(int i = 0; i < size; i++){
        num.push_back(number[i]-48);
    }
    for(int i = 0; i < 10; i++){
        cin >> hash[i];
    }
    int first = -1;
    int second = -1;
    for(int i = 0; i < size; i++){
        if(num[i]<=hash[num[i]-1]){
            if(first == -1 && num[i]!=hash[num[i]-1]){
                first = i;
            }
        }
        else{
            if(first != -1){
                second = i;
                break;
            }
        }
    }
    if(second == -1){
        second = size;
    }
    if(first == -1){
        cout << number;
    }
    else{
        for(int i = first; i < second; i++){
            num[i]= hash[num[i]-1];
        }
        for(int i = 0; i < size; i++){
            cout << num[i];
        }
    }
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(j+c<n-1)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                     if(d==0&&c<9&&j+c<n-1)
                      {

                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;

                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          {s[j+c+1]=char(a[l+c]+48);}
                          c++;

                      }
                      else  break;
                  }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
using namespace std;
int main()
{

    int n,k=0;
    cin>>n;
    string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    {
        cin>>a[i];
        if(a[i]>i+1)
           {
               for(int j=k;j<s.length();j++)
               if(int(s[j])==i+49)
               {
                s[j]=char(a[i]+48);
                k=j+1;
               }
           }
    }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,k=0;
    cin>>n;
    string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    {
        cin>>a[i];
        if(a[i]>i+1)
           {
               for(int j=k;j<n;j++)
               if(int(s[j])==i+49)
               {
                s[j]=char(a[i]+48);
                k=j+1;
               }
           }
    }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                    {
                      if(d==0&&c<9&&j+c<n-1)
                      {
                      cout<<j+1<<"element="<<s[j]<<endl;
                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;

                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          s[j+c+1]=char(a[l+c]+48);c++;
                      }
                      break;
                    }
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
        cout<<"1."<<l<<endl;
                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                      if(d==0&&c<9&&j+c<n-1)
                      {
                      int x=s[j+1+c];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          s[j+c+1]=char(a[l+c]+48);
                          c++;

                     }
                     else break;
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                { s[j]=char(a[l-1]+48);d++;}
                else if(d!=0&&a[l-1]==l);
                else if(d!=0)break;
        }
        cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,m;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                {
                    m=s[j];
                s[j]=char(a[l-1]+48);
                if(s[j+1]!=m) break;
                }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                { s[j]=char(a[l-1]+48);d++;}
                else if(d!=0&&a[l-1]==l);
                else id(d!=0)break;
        }
        cout<<s;
}


#include<iostream>
using namespace std;
int main()
{

    int n,k=0;
    cin>>n;
    char s[n];
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    {
        cin>>a[i];
        if(a[i]>i+1)
           {
               for(int j=k;j<n;j++)
               if(int(s[j])==i+49)
               {
                s[j]=char(a[i]+48);
                k=j+1;
               }
           }
    }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                {
                  int c=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(j+c<n-1&&c<8)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;
                    }
                    else
                     if(d==0&&c<8&&j+c<n-1)
                      {
                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min>=c+1&&c<9)
                        if(a[x-49]>x-48)
                          s[j+c+1]=char(a[x-49]+48);
                          c++;

                      }
                      else break;
                  }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;
                    }
                    else
                      if(d==0&&c<9&&j+c<n-1)
                      {
                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          s[j+c+1]=char(a[l+c]+48);

                     }
                     else break;
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                    {
                      if(d==0&&c<9&&j+c<n-1)
                      {

                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;

                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          s[j+c+1]=char(a[l+c]+48);
                         c++;

                      }
                      break;
                    }
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                {
                  int c=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(j+c<n-1&&c<8)
                  { cout<<"while loop "<<endl;
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;
                    }
                    else
                     if(d==0&&c<8&&j+c<n-1)
                      {
                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min>=c+1&&c<9)
                        if(a[x-49]>x-48)
                          s[j+c+1]=char(a[x-49]+48);
                          c++;

                      }
                      else break;
                  }
                break;
               }
        }
    cout<<s;
}


#include<string>
#include<iostream>
using namespace std;
int main()
{

    int n;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                s[j]=char(a[l-1]+48);
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>=l)
                { s[j]=char(a[l-1]+48);d++;}
                else if(d!=0) break;
        }
        cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(j+c<n-1&&c<8)
                  {
                    if(s[j+c+1]==k&&max-min==0)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                     if(d==0&&c<8&&j+c<n-1)
                      {

                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;

                        if(max-min==c+1)
                        if(a[x-49]>x-48)
                          {s[j+c+1]=char(a[x-49]+48);}
                         c++;

                      }
                      else  break;
                  }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
using namespace std;
int main()
{

    int n,k=0;
    cin>>n;
    string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    {
        cin>>a[i];
        if(a[i]>i+1)
           {
               for(int j=k;j<n;j++)
               if(int(s[j])==i+49)
               {
                s[j]=char(a[i]+48);
                k=j+1;
               }
           }
    }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  while(m==0)
                  {
                    if(s[j+c+1]==s[j+c])
                    { s[j+c]=char(a[l-1+c]+48);
                      c++;d++;

                    }
                    else
                      if(d==0&&c<9&&j+c<n-1)
                      { s[j]=char(a[l-1]+48);

                        if(int(s[j+1+c])-48>max) max=int(s[j+c+1]-48);
                        if(int(s[j+1+c])-48<min) min=int(s[j+c+1]-48);

                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          {s[j+c+1]=char(a[l+c]+48);cout<<"aya to hai"<<endl;}

                     }
                     else break;
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
using namespace std;
int main()
{

    int n,k=0;
    cin>>n;
    string s;
    getline(cin,s);
    int a[9];
    for(int i=0;i<9;i++)
    {
        cin>>a[i];
        if(a[i]>i+1)
           {
               for(int j=k;j<n;j++)
               if(int(s[j])==i+49)
               {
                s[j]=char(a[i]+48);
                k=j+1;
               }
           }
    }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,m;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                { m=s[j];
                s[j]=char(a[l-1]+48);
                if(s[j+1]!=m) break;
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                      if(d==0&&c<9&&j+c<n-1)
                      {

                        if(int(s[j+1+c])-48>max) max=int(s[j+c+1]-48);
                        if(int(s[j+1+c])-48<min) min=int(s[j+c+1]-48);

                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          {s[j+c+1]=char(a[l+c]+48);}

                     }
                     else break;
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                { s[j]=char(a[l-1]+48);d++;}
                else if(d!=0) break;
        }
        cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;
                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(j+c<n-1&&c<8)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;
                    }
                    else
                     if(d==0&&c<9&&j+c<n-1)
                      {
                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min>=c+1)
                        if(a[x-49]>x-48)
                         s[j+c+1]=char(a[x-49]+48);
                          c++;

                      }
                      else break;
                  }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

    int n,d=0;
    cin>>n;
  string s;
    cin>>s;
    int a[9];
    for(int i=0;i<9;i++)
    cin>>a[i];
        for(int j=0;j<n;j++)
        {  int l=int(s[j])-48;

                if(a[l-1]>l)
                {
                  int c=0,m=0,max=l,min=l;
                  char k=s[j];
                  s[j]=char(a[l-1]+48);
                  while(m==0)
                  {
                    if(s[j+c+1]==k)
                    { s[j+c+1]=char(a[l-1]+48);
                      c++;d++;

                    }
                    else
                    {
                      if(d==0&&c<9&&j+c<n-1)
                      {

                      int x =s[j+c+1];
                        if(x-48>max) max=x-48;
                        if(x-48<min) min=x-48;
                        if(max-min==c+1)
                        if(a[l+c]>l+c+1)
                          {s[j+c+1]=char(a[l+c]+48);}
                          c++;

                     }
                     if(j+c==n-1) break;
                  }
                 }
                break;
               }
        }
    cout<<s;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n;
	int a[200005];
	int re[10],book;
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%1d",&a[i]);
	for(int i=1;i<=9;i++)
	cin>>re[i];
	for(int i=1;i<=n;i++)
	{
		if(a[i]<re[a[i]])
		{
			book=i;
			a[i]=re[a[i]];
			break;
		}
	}
	for(int i=book+1;i<=n;i++)
	{
		if(a[i]<=re[a[i]])
		a[i]=re[a[i]];
		else
		break;
	}
	for(int i=1;i<=n;i++)
	cout<<a[i];

	 cin>>n;
	return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n;
	int a[200005];
	int re[10],book;
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%1d",&a[i]);
	for(int i=1;i<=9;i++)
	cin>>re[i];
	for(int i=1;i<=n;i++)
	{
		if(a[i]<re[a[i]])
		{
			book=i;
			a[i]=re[a[i]];
			break;
		}
	}
	for(int i=book+1;i<=n;i++)
	{
		if(a[i]<re[a[i]])
		a[i]=re[a[i]];
		else
		break;
	}
	for(int i=1;i<=n;i++)
	cout<<a[i];

	 cin>>n;
	return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n;
	int a[200005];
	int re[10],book;
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%1d",&a[i]);
	for(int i=1;i<=9;i++)
	cin>>re[i];
	for(int i=1;i<=n;i++)
	{
		if(a[i]<re[a[i]])
		{
			book=a[i];
			break;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]==book)
		a[i]=re[a[i]];
	}
	for(int i=1;i<=n;i++)
	cout<<a[i];

	 cin>>n;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>

#define ll long long
#define ld long double
#pragma comment (linker, "/STACK:5000000000")
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
int  Inf = 2e9;
ll LINF = (ll)4e18;
using namespace std;
ll mod = 1e9 + 7;
#define M_PI 3.14159265358979323846   // pi

int fun(int n){
    int k=n+1;
    while (k%10==0)
        k/=10;
    return k;
}
int main(int argc, const char * argv[])
{
    ///////////////Conect File////////////////////////
    for(int i = 0; i < argc; i++){
        if((string)argv[i] == "_DEBUG"){
            freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
        }
    }
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    map<int, int> mp;
    for(int i=0; i<9; i++){
        int q;
        cin>>q;
        mp[i+1]=q;
    }
    int st=0;
    for(int i=0; i<n; i++){

        if (s[i]-'0'<=mp[s[i]-'0']){
            if (s[i]-'0' == mp[s[i]-'0']){
                if (st!=0) st++;
            }else{
                st++;
                s[i]=char(mp[s[i]-'0']+'0');

            }
        } else{
            if (st!=0){
                cout<<s;
                return 0;
            }
        }
    }
    cout<<s;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	for(int i=0;i<n;i++){
		ans[i]=input[i];
		if(last!=ans[i])continue;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
			}
			input[n-1]=last;
		}
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=1;i--){
		char t=ans[i+1];

		if(last!=ans[i])continue;
		ans[i+1]=0;
		if(find(input+1,n-1,ans,i,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i,SA,RMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=1;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;

typedef long long int lli;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=(x);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)

long long mod_pow(long long a,long long n,long long M){
	if(n==0)return 1;
	long long res=mod_pow(a*a%M,n/2,M);
	if(n&1)res=res*a%M;
	return res;
}
//任意の区間のハッシュ文字列をO(1)で求める
//前処理はO(L log L)
const int P1=293,P2=1000000009;//P2は素数,P1も素数が望ましい
const int MAX_L=1111111;
int pow_P1[MAX_L],rev_P1[MAX_L];
int str_hash[MAX_L];
void init_hash(const string &in){
	for(int i=0;i<MAX_L;i++){
		pow_P1[i]=mod_pow(P1,i,P2);
	}
	for(int i=0;i<MAX_L;i++){
		rev_P1[i]=mod_pow(pow_P1[i],P2-2,P2);
	}
	*str_hash=0;
	REP(i,in.size()){
		str_hash[i+1]=(str_hash[i]+(lli)pow_P1[i]*in[i])%P2;
	}
}
//文字列の[s,e)に関するハッシュ値を求める
int calc_hash(int s,int e){
	return ((lli)(((lli)str_hash[e]-str_hash[s]+P2)%P2)*rev_P1[s])%P2;
}

//ローリングハッシュ
template <int B,int M>
class RollingHash{
private:
	int value;
	int p;//ハッシュの長さ
	int hash_back;
public:
	int operator()() const{
		return value;
	}
	void set_length(int p_){
		hash_back=mod_pow(B,p_-1,M);p=p_;
	}
	explicit RollingHash(int p=0):p(p){value=0;set_length(p);}
	void add(int v){//後ろにvを追加する
		value=((long long)value*B+v)%M;
	}
	void remove(int v){
		value=(((long long)value+M-(long long)hash_back*v%M))%M;
	}
	bool operator==(const RollingHash &a){
		return a.value==value;
	}
};

bool rabin_karp(const char *exp,const char *find){
	int m=strlen(find),i,n=strlen(exp);
	RollingHash<293,1000000009> find_hash(m),exp_hash(m);
	if(n<m)return false;
	for(i=0;i<m;i++){
		find_hash.add(find[i]);
		exp_hash.add(exp[i]);
	}
	i=0;
	while(1){
		if(find_hash()==exp_hash()){
			return true;
		}
		if(i>=n-m)break;
		exp_hash.remove(exp[i]);
		exp_hash.add((exp[i+m]));
		i++;
	}
	return false;
}
string in;
//二分探索
int max_length(string &in){
	char t=in[in.length()-1];
	in[in.length()-1]='!';
	int low=0,high=in.size();
	while(low+1<high){
		int mid=(low+high)/2;
		string pre=in.substr(0,mid);
		if(rabin_karp(in.c_str()+1,pre.c_str())){
			low=mid;
		}else{
			high=mid;
		}
	}
	in[in.length()-1]=t;
	return low;
}
int main(){
	cin>>in;
	int pre_len=max_length(in);
	init_hash(in);

	for(int i=pre_len;i>=1;i--){
		//cout<<calc_hash(0,i)<<endl;
		//cout<<calc_hash(in.size()-i,in.size())<<endl;
		if(calc_hash(0,i)==calc_hash(in.size()-i,in.size())){
			cout<<in.substr(0,i)<<endl;
			return 0;
		}
	}
	cout<<"Just a legend"<<endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int n;
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	if(a==n-1)
	return strncmp(t+sa[a+1], p, m) == 0 ? 1 : 0;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	return rh == m ? sa[r] : -1;
}
char input[1111111];
char ans  [1111111];
int anslen=0;
int main() {
	gets(input);
	n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;

	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);

	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=0;i<=n-2;i++){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;

		int tmp=find(input+1,n-1,ans,i+1,sufSA,sufRMQ);
		if(tmp==-1)break;
		if(tmp+1==n-i-1){
			input[n-1]=0;
			int tmp=find(input+1,n-2,ans,i+1,SA,RMQ);
			if(tmp==-1)break;
			if(tmp!=-1){
				anslen=i+1;
			}
			input[n-1]=last;
		}

		ans[i+1]=t;
	}
	//2回含まれる
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	for(int i=0;i<n;i++){
		ans[i]=input[i];
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
			}
			input[n-1]=last;
		}
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}