
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//http://p...content-available-to-author-only...j.com/problems/ZABAWA/

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().zadanie();
	}

	private void zadanie() throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s;
        String[] tab;
        int x, y;
                        
        while ((s = in.readLine()) != null) { 
                tab = s.split(" ");
                x = Integer.parseInt(tab[0]);
                y = Integer.parseInt(tab[1]);
			if (isPrime(x)) {
				y++;
				while (true) {
					if (isPalidrome(y)) {
						System.out.println(y);
						break;
					} else {
						y++;
					}
				}
			} else {
				y++;
				while (true) {
					if (isPalidrome(y)) {
						System.out.println(y);
						break;
					} else {
						y--;
					}
				}
			}
		}
	}

	private boolean isPrime(int n) {
		for (int i = 2; i < Math.sqrt(n)+1; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}

	private boolean isPalidrome(int n) {
		String s = n + "";
		return new StringBuilder(s).reverse().toString().equals(s) ? true : false;
	}
}
