// Arup Guha
// 8/2/2021
// Solution to 2021 UCF Qualifying Contest Problem: Palindromic Primes

import java.util.*;

public class palprimes {

	public static ArrayList<Integer> pList;
	
	public static void main(String[] args) {
		
		// Get range.
		Scanner stdin = new Scanner(System.in);
		long low = stdin.nextLong();
		long high = stdin.nextLong();
	
		// Run Prime Sieve upto 999,999
		boolean[] sieve = new boolean[1000000];
		Arrays.fill(sieve, true);
		for (int i=2; i*i<sieve.length; i++) {
			if (!sieve[i]) continue;
			for (int j=2*i; j<sieve.length; j+=i)
				sieve[j] = false;
		}
	
		// Store primes to 999,999
		pList = new ArrayList<Integer>();
		for (int i=2; i<sieve.length; i++)
			if (sieve[i])
				pList.add(i);
		
		// Just store all palindromic primes in range here.
		ArrayList<Long> mylist = new ArrayList<Long>();
		mylist.add(2L); mylist.add(3L); mylist.add(5L); mylist.add(7L); mylist.add(11L);
		
		// All of these have an odd # of digits, and start with 1,3,7 or 9, so just check those.
		for (int i=10; i<1000000; i++) {
			
			// Can't be prime.
			if (msd(i) == 5 || msd(i)%2 == 0) continue;
			
			// Form the number and add it if it's prime.
			long flipNo = makePal(i);
			if (isPrime(flipNo)) { mylist.add(flipNo); }
		}
		
		// The other work before was slow compared to this...
		// If I had many queries in a single case, I could force a more efficient method than this.
		int res = 0;
		for (Long x: mylist)
			if (x >= low && x <= high)
				res++;
		System.out.println(res);
	}
	
	// Returns and odd length palindrome with the most significant digits being n. 
	public static long makePal(int n) {
		
		// Already put this in the result.
		long res = n;
		
		// Get rid of the middle digit, which is the only one not added twice.
		n /= 10;
		
		// Peel off the rest of the digits and "concatenate" them onto res.
		while (n > 0) {
			res = (10*res) + n%10;
			n /= 10;
		}
		
		// Ta da!
		return res;
	}
	
	// Returns the most significant digit of n.
	public static int msd(int n) {
		while (n >= 10) n /= 10;
		return n;
	}
	
	// Returns true iff n is prime. Assumes n <= 1,000,000,000,000.
	public static boolean isPrime(long n) {
		if (n == 1) return false;
		for (int i=0; i<pList.size() && ((long)pList.get(i))*pList.get(i)<=n; i++)
			if (n%pList.get(i) == 0)
				return false;
		return true;
	}
}
