#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll int
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define MAXN 100010
using namespace std;
ll used[MAXN];
ll usedd[MAXN];
ll check(ll n,ll sum)
{
ll s=0;
while(n>0)
{
ll q=n%10;
s+=q;
n/=10;
}
if(s==sum)return 1;
else return 0;
}
ll checkprime(ll n)
{
if(n==2 || n==3)return 1;
else if(n==1 || n%2==0 || n%3==0)return 0;
for(ll i=5,p=2;i*i<=n;i+=p,p=6-p)
{
if(n%i==0)return 0;
}
return 1;
}
vector<ll>primes;
char table[8][8];
void print()
{
for(ll i=1;i<=5;i++)
{
for(ll j=1;j<=5;j++)
{
printf("%d",table[i][j]);
}
printf("\n");
}
}
vector<ll>massiv;
ll kol=0;
int main(){
ll sum,x;
scanf("%d%d",&sum,&x);
for(ll i=10000;i<=99999;i++)
{
if(checkprime(i))
{
if(check(i,sum))
{
used[i]=1;
primes.pb(i);
}
}
}
ll one,two,three,four,five;
vector<ll>pos;
for(ll i=0;i<primes.size();i++)
{
if(primes[i]/10000==x)
{
pos.pb(primes[i]);
}
}
if((ll)pos.size()==0 || (ll)primes.size()==0)
{
cout<<"NONE"<<endl;
return 0;
}
ll sd=0;
for(ll i=0;i<(ll)pos.size();i++)
{
ll diagonalone=pos[i];
table[1][1]=diagonalone/10000;
table[2][2]=diagonalone/1000%10;
table[3][3]=diagonalone/100%10;
table[4][4]=diagonalone/10%10;
table[5][5]=diagonalone%10;
vector<ll>onee;
vector<ll>twoo;
vector<ll>threee;
vector<ll>fourr;
vector<ll>fivee;
for(ll j=0;j<primes.size();j++)
{
ll cis=primes[j];
if(cis/10000==table[1][1])
{
onee.pb(cis);
}
if(cis/10000==table[1][1])
{
onee.pb(cis);
}
if(cis/1000%10==table[2][2])
{
twoo.pb(cis);
}
if(cis/100%10==table[3][3])
{
threee.pb(cis);
}
if(cis/10%10==table[4][4])
{
fourr.pb(cis);
}
if(cis%10==table[5][5])
{
fivee.pb(cis);
}
}
for(ll ii=0;ii<onee.size();ii++)
{
one=onee[ii];
table[1][2]=one/1000%10;
table[1][3]=one/100%10;
table[1][4]=one/10%10;
table[1][5]=one%10;
for(ll iii=0;iii<twoo.size();iii++)
{
two=twoo[iii];
table[2][1]=two/10000;
table[2][3]=two/100%10;
table[2][4]=two/10%10;
table[2][5]=two%10;
for(ll iiii=0;iiii<threee.size();iiii++)
{
three=threee[iiii];
table[3][1]=three/10000;
table[3][2]=three/1000%10;
table[3][4]=three/10%10;
table[3][5]=three%10;
for(ll iiiii=0;iiiii<fourr.size();iiiii++)
{
four=fourr[iiiii];
table[4][1]=four/10000;
table[4][2]=four/1000%10;
table[4][3]=four/100%10;
table[4][5]=four%10;
for(ll iiiiii=0;iiiiii<fivee.size();iiiiii++)
{
five=fivee[iiiiii];
table[5][1]=five/10000;
table[5][2]=five/1000%10;
table[5][3]=five/100%10;
table[5][4]=five/10%10;
ll ones=table[1][1]*10000+table[2][1]*1000+table[3][1]*100+table[4][1]*10+table[5][1];
ll twos=table[1][2]*10000+table[2][2]*1000+table[3][2]*100+table[4][2]*10+table[5][2];
ll threes=table[1][3]*10000+table[2][3]*1000+table[3][3]*100+table[4][3]*10+table[5][3];
ll fours=table[1][4]*10000+table[2][4]*1000+table[3][4]*100+table[4][4]*10+table[5][4];
ll fives=table[1][5]*10000+table[2][5]*1000+table[3][5]*100+table[4][5]*10+table[5][5];
ll diagonaltwo=table[1][5]*10000+table[2][4]*1000+table[3][3]*100+table[4][2]*10+table[5][1];
if(used[ones] && used[twos] && used[threes] && used[fours] && used[fives] && used[diagonaltwo] )
{
if(usedd[ones]!=1 || usedd[twos]!=1 || usedd[threes]!=1 || usedd[fours]!=1 || usedd[fives]!=1 || usedd[diagonaltwo]!=1)
{
usedd[ones]=1,usedd[twos]=1,usedd[threes]=1,usedd[fours]=1,usedd[f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7CiAgIGZvcihsbCBqPTA7ajxwcmltZXMuc2l6ZSgpO2orKykKICAgewogICAgIGxsIGNpcz1wcmltZXNbal07CiAgICAgaWYoY2lzLzEwMDAwPT10YWJsZVsxXVsxXSkKICAgICB7CiAgICAgICBvbmVlLnBiKGNpcyk7CiAgICAgfQogICAgIGlmKGNpcy8xMDAwMD09dGFibGVbMV1bMV0pCiAgICAgewogICAgICAgb25lZS5wYihjaXMpOwogICAgIH0KICAgICBpZihjaXMvMTAwMCUxMD09dGFibGVbMl1bMl0pCiAgICAgewogICAgICAgdHdvby5wYihjaXMpOwogICAgIH0KICAgICBpZihjaXMvMTAwJTEwPT10YWJsZVszXVszXSkKICAgICB7CiAgICAgICB0aHJlZWUucGIoY2lzKTsKICAgICB9CiAgICAgaWYoY2lzLzEwJTEwPT10YWJsZVs0XVs0XSkKICAgICB7CiAgICAgICBmb3Vyci5wYihjaXMpOwogICAgIH0KICAgICBpZihjaXMlMTA9PXRhYmxlWzVdWzVdKQogICAgIHsKICAgICAgIGZpdmVlLnBiKGNpcyk7CiAgICAgfQogICB9CiAgIGZvcihsbCBpaT0wO2lpPG9uZWUuc2l6ZSgpO2lpKyspCiAgIHsKICAgICBvbmU9b25lZVtpaV07CiAgICAgdGFibGVbMV1bMl09b25lLzEwMDAlMTA7CiAgICAgdGFibGVbMV1bM109b25lLzEwMCUxMDsKICAgICB0YWJsZVsxXVs0XT1vbmUvMTAlMTA7CiAgICAgdGFibGVbMV1bNV09b25lJTEwOwogICAgIGZvcihsbCBpaWk9MDtpaWk8dHdvby5zaXplKCk7aWlpKyspCiAgICAgewogICAgICAgdHdvPXR3b29baWlpXTsKICAgICAgIHRhYmxlWzJdWzFdPXR3by8xMDAwMDsKICAgICAgIHRhYmxlWzJdWzNdPXR3by8xMDAlMTA7CiAgICAgICB0YWJsZVsyXVs0XT10d28vMTAlMTA7CiAgICAgICB0YWJsZVsyXVs1XT10d28lMTA7CiAgICAgICBmb3IobGwgaWlpaT0wO2lpaWk8d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