#include<iostream>
#include<cstdlib>
using namespace std;
enum Boolean{FALSE,TRUE};
Boolean ispal(int a)
{
int num=a;
int digit=0,rev=0;
while(a>0)
{
digit=a%10;
rev=rev*10+digit;
a=a/10;
}
if(num==rev)
return TRUE;
else
return FALSE;
}
Boolean isprime(int n)
{
int flag=0;
for(int i=1;i<=n/2;i++)
{
if((n%i)==0)
flag++;
}
if(flag>1)
return FALSE;
else
return TRUE;
}
int main()
{
int input, output;
cin>>input;
for(long int i=input+1;i<10000000;i++)
{
if(isprime(i))
{
if(ispal(i))
{
cout<<i;
exit(0);
}
else
continue;
}
continue;
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGNzdGRsaWI+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmVudW0gQm9vbGVhbntGQUxTRSxUUlVFfTsKQm9vbGVhbiBpc3BhbChpbnQgYSkKewogIGludCBudW09YTsKICBpbnQgZGlnaXQ9MCxyZXY9MDsKICB3aGlsZShhPjApCiAgICB7CiAgICAgIGRpZ2l0PWElMTA7CiAgICAgIHJldj1yZXYqMTArZGlnaXQ7CiAgICAgIGE9YS8xMDsKICAgIH0KICBpZihudW09PXJldikKICAgIHJldHVybiBUUlVFOwogIGVsc2UgCiAgICByZXR1cm4gRkFMU0U7Cn0KQm9vbGVhbiBpc3ByaW1lKGludCBuKQp7CiAgaW50IGZsYWc9MDsKICBmb3IoaW50IGk9MTtpPD1uLzI7aSsrKQogICAgewogICAgICBpZigobiVpKT09MCkKICAgIGZsYWcrKzsKICAgIH0KICBpZihmbGFnPjEpCiAgICByZXR1cm4gRkFMU0U7CiAgZWxzZQogICAgcmV0dXJuIFRSVUU7Cn0KaW50IG1haW4oKQp7CiAgaW50IGlucHV0LCBvdXRwdXQ7CiAgY2luPj5pbnB1dDsKICBmb3IobG9uZyBpbnQgaT1pbnB1dCsxO2k8MTAwMDAwMDA7aSsrKQogICAgewogICAgICBpZihpc3ByaW1lKGkpKQogICAgICB7CiAgICBpZihpc3BhbChpKSkKICAgICAgewogICAgICAgIGNvdXQ8PGk7CiAgICAgICAgZXhpdCgwKTsKICAgICAgfQogICAgZWxzZQogICAgICBjb250aW51ZTsKICAgICAgfQogICAgY29udGludWU7CiAgICB9CiAgcmV0dXJuIDA7Cn0=