#include
#include
#include
#include
#include
using namespace std;
int main()
{
int N;
cin>>N;
string gh="02468";
map arr;
map gg;
int count=0;
for(int i=13;i<1000000&&count
{
string rec=to_string(i);
if(gh.find(rec[0])!=string::npos||gh.find(*rec.rbegin())!=string::npos)
continue;
bool sign=false;
if(gg[i]!=0)
{
count+=1;
arr[count]=i;
continue;
}
auto it1=rec.begin();
auto it2=rec.end()-1;
for(;it1
{
if(*it1!=*it2)
{
sign=true;
break;
}
}
if(!sign) continue;
int temp=sqrt(i);
bool sign2=true;
for(int j=2;j<=temp+1;j++)
{
if(!(i%j))
{sign2=false;break;}
}
if (!sign2) continue;
reverse(rec.begin(),rec.end());
int rev=stoi(rec);
temp=sqrt(rev);
if (gg[rev]!=0)
{
count++;
arr[count]=i;
continue;
}
for(int j=2;j<=temp+1;j++)
{
if(!(rev%j))
{
sign2=false;
break;
}
}
if(sign2)
{
gg[rev]=1;
gg[i]=1;
count++;
arr[count]=i;
}
}
if(arr[N]!=0) cout<
else cout<
}