Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem G
Do Geese See God?

A palindrome is a sequence of characters which reads the same backward or forward. Famous ones include "dogeeseseegod" ("Do geese see God?"), "amoreroma" ("Amore, Roma.") and "risetovotesir" ("Rise to vote, sir.").

An ancient sutra has been handed down through generations at a temple on Tsukuba foothills. They say the sutra was a palindrome, but some of the characters have been dropped through transcription.

A famous scholar in the field of religious studies was asked to recover the original. After long repeated deliberations, he concluded that no information to recover the original has been lost, and that the original can be reconstructed as the shortest palindrome including all the characters of the current sutra in the same order. That is to say, the original sutra is obtained by adding some characters before, between, and/or behind the characters of the current.

Given a character sequence, your program should output one of the shortest palindromes containing the characters of the current sutra preserving their order. One of the shortest? Yes, more than one shortest palindromes may exist. Which of them to output is also specified as its rank among the candidates in the lexicographical order.

For example, if the current sutra is cdba and 7th is specified, your program should output cdabadc among the 8 candidates, abcdcba, abdcdba, acbdbca, acdbdca, cabdbac, cadbdac, cdabadc and cdbabdc.

Input

The input consists of a single test case. The test case is formatted as follows.

$S$

$k$

The first line contains a string $S$ composed of lowercase letters from 'a' to 'z'. The length of $S$ is greater than 0, and less than or equal to 2000. The second line contains an integer $k$ which represents the rank in the lexicographical order among the candidates of the original sutra. $1 \leq k \leq 10^{18}$.

Output

Output the $k$-th string in the lexicographical order among the candidates of the original sutra. If the number of the candidates is less than $k$, output NONE.

Though the first lines of the Sample Input/Output 7 are folded at the right margin, they are actually single lines.

Sample Input 1

crc
1

Sample Output 1

crc

Sample Input 2

icpc
1

Sample Output 2

icpci

Sample Input 3

hello
1

Sample Output 3

heolloeh

Sample Input 4

hoge
8

Sample Output 4

hogegoh

Sample Input 5

hoge
9

Sample Output 5

NONE

Sample Input 6

bbaaab
2

Sample Output 6

NONE

Sample Input 7

thdstodxtksrnfacdsohnlfuivqvqsozdstwa
szmkboehgcerwxawuojpfuvlxxdfkezprodne
ttawsyqazekcftgqbrrtkzngaxzlnphynkmsd
sdleqaxnhehwzgzwtldwaacfczqkfpvxnalnn
hfzbagzhqhstcymdeijlbkbbubdnptolrmemf
xlmmzhfpshykxvzbjmcnsusllpyqghzhdvljd
xrrebeef
11469362357953500

Sample Output 7

feeberrthdstodxtksrnfacdjsohnlfuivdhq
vqsozhgdqypllstwausnzcmjkboehgcerzvwx
akyhswuojpfhzumvmlxxdfkmezmprlotpndbu
bbkblnjiedttmawsyqazekcftgshqbrhrtkzn
gaxbzfhnnlanxvphyfnkqmzcsdfscaawdleqa
xtnhehwzgzwhehntxaqeldwaacsfdsczmqknf
yhpvxnalnnhfzbxagnzktrhrbqhsgtfckezaq
yswamttdeijnlbkbbubdnptolrpmzemkfdxxl
mvmuzhfpjouwshykaxwvzrecgheobkjmcznsu
awtsllpyqdghzosqvqhdviuflnhosjdcafnrs
ktxdotsdhtrrebeef

Solution in C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
typedef pair<int,ll> P;
ll inf=1e18;
string s;
ll K;
P dp[2000][2001][2];
int N;
void change(P& x,P y){
	y.fs+=2;
	if(x.fs>y.fs) x=y;
	else if(x.fs==y.fs) x.sc+=y.sc,chmin(x.sc,inf);
}
int main(){
	cin>>s>>K;
	N=s.size();
	rep(i,N) rep(j,N+1) rep(k,2) dp[i][j][k]=P(1e9,0);
	rep(i,N) dp[i][i][0]=P(0,1);
	rep(i,N) dp[i][i+1][1]=P(1,1);
	for(int d=2;d<=N;d++){
		rep(l,N+1-d) rep(e,2){
			int r=l+d;
			if(s[l]==s[r-1]){
				change(dp[l][r][e],dp[l+1][r-1][e]);
			}else{
				change(dp[l][r][e],dp[l][r-1][e]);
				change(dp[l][r][e],dp[l+1][r][e]);
			}
		}
	}
	int e=1;
	if(dp[0][N][0].fs<dp[0][N][1].fs) e=0;
	int l=0,r=N,len=dp[0][N][e].fs;
	if(dp[0][N][e].sc<K){
		puts("NONE");
		return 0;
	}
	string ans(len,'?');
	int i=0;
	while(true){
		if(r-l==1){
			ans[i++]=s[l];
			break;
		}
		if(r==l) break;
		len-=2;
		if(s[l]==s[r-1]){
			ans[i++]=s[l];
			l++,r--;
			continue;
		}else if(s[l]<s[r-1]){
			if(dp[l+1][r][e].fs==len){
				if(dp[l+1][r][e].sc>=K){
					ans[i++]=s[l];
					l++;
					continue;
				}else{
					ans[i++]=s[r-1];
					K-=dp[l+1][r][e].sc;
					r--;
					continue;
				}
			}else{
				ans[i++]=s[r-1];
				r--;
			}
		}else{
			if(dp[l][r-1][e].fs==len){
				if(dp[l][r-1][e].sc>=K){
					ans[i++]=s[r-1];
					r--;
					continue;
				}else{
					ans[i++]=s[l];
					K-=dp[l][r-1][e].sc;
					l++;
					continue;
				}
			}else{
				ans[i++]=s[l];
				l++;
			}
		}
	}
	int L=ans.size();
	rep(i,L) ans[L-1-i]=ans[i];
	cout<<ans<<endl;
}