#include <cstdio>
#include <vector>
#include <string>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
// Generate the palindromes
void gen(int seed, int parity, int l, int h, vector<int> & palindromes){
// Generates a single palindrome
// parity = 0: even length
// parity = 1: odd length
string str = to_string(seed);
string reversed = str.substr(0,str.length()-parity);
reverse(reversed.begin(),reversed.end());
int pal = stoi(str+reversed);
if(pal>=l && pal<=h){
palindromes.push_back(pal);
}
}
void generate(int l, int h, vector<int> & palindromes){
for(int j=0; j<4; j++){
for(int i=pow(10,j); i<pow(10,j+1); i++){
gen(i,0,l,h,palindromes);
gen(i,1,l,h,palindromes);
}
}
}
// Get the primes
void getPrimes(vector<int> palindromes, vector<int> & primes){
bool k;
for(int pal:palindromes){
k = true;
for(int fact=2;fact<sqrt(pal)+1;fact++){
if(pal%fact == 0){
k=false;
break;
}
}
if(k==true){
primes.push_back(pal);
}
}
}
// Main, obviously
int main(){
// Read the input file
ifstream fIn("pprime.in");
int l, h;
fIn >> l >> h;
fIn.close();
// Do the important things
vector<int> palindromes;
vector<int> primes;
generate(l, h, palindromes);
getPrimes(palindromes,primes);
sort(primes.begin(),primes.end());
// Write the output file
ofstream fOut("pprime.out");
for(int i:primes){
fOut << i << endl;
}
fOut.close();
return 0;
}