#include<stdio.h>
int prime(long int a);
int palindrome(long int a);
int main()
{
long int a;
scanf("%ld",&a);
if(a <= 1000000)
while(1)
{
if(palindrome(a)){
if(prime(a)) {printf("%ld",a);return;}
}
a=a+1;
}
return 0;
}
int prime(long int a){
long int i;
for(i=2;i<=a/2;i++) if(a%i==0)return 0;
return 1;
}
int palindrome(long int a){
long int temp=a,reverse=0;
while( temp != 0 ){
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if(reverse == a) return 1;
else return 0;
}