#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int N = 100;
#define int long long
int f[N][10][2];
bool ck(int x, int y) {
if (x < 0) return 1;
if (x + y< 2) return false;
x += y ;
for (int i = 2; i <= x / i; i ++)
if (x % i == 0) return false;
return true;
}
void init () {
f[1][1][1] = 1;
f[1][0][0] = 1;
for (int i = 2; i <= 9; i ++)
f[1][i][0] = 1;
for (int i = 2; i < N; i ++)
for (int j = 0; j <= 9; j ++)
for (int k = 0; k <= 9; k ++)
if (ck(j, k)) {
if (j == 1) {
f[i][j][1] += f[i - 1][k][0] + f[i - 1][k][1];
f[i][j][0] = 0;
}
else {
f[i][j][1] += f[i - 1][k][1];
f[i][j][0] += f[i - 1][k][0];
}
}
}
int dp (int n) {
if (!n ) return 0;
vector<int> nums;
int res = 0, last = -1;//第一次一定满足条件
while (n) nums.push_back (n % 10), n /= 10;
bool ok = 0;
for (int i = nums.size() - 1; i >= 0; i --) {
int x = nums[i];
for (int j = (i == nums.size() - 1) ; j < x; j ++) {
if (ck(last, j) ){
res += f[i + 1][j][1];
if (ok) res += f[i + 1][j][0];
}
}
if (x == 1) ok = 1;
if (ck(last, x)) last = x;
else break;
if (!i&& ok) res ++;
}
for (int i = 1; i < nums.size(); i ++)
for (int j = 1; j <= 9; j ++)
res += f[i][j][1];
return res;
}
signed main () {
init();
int l, r;
cin >> l >> r;
cout << dp(r) - dp(l - 1) << endl;
//cout << dp (r) - dp (l - 1) << endl;
}