#include using namespace std; using uint = unsigned int; using ll = long long; using pii = pair; #define dbg(x) cerr<<#x": "<<(x)<<'\n' #define dbg_v(x, n) cerr<<#x"[]: ";for(long long _=0;_ primes; bool isPrime[VMAX]; bool checkIsPrime(int x) { if(x < VMAX) return isPrime[x]; for(auto it = primes.begin(); it != primes.end() && (*it) * (*it) <= x; ++it) if(x % (*it) == 0) return false; return true; } bool ok(int x) { if(!checkIsPrime(x)) return false; int i, j, n, v[10]; for(n = 0; x; x /= 10, ++n) v[n] = x % 10; reverse(v, v + n); for(i = 1; i < (1 << n); ++i) { for(x = 0, j = 0; j < n; ++j) if(i & (1 << j)) x = x * 10 + v[j]; if(!checkIsPrime(x)) return false; } return true; } void gen(int x, int n) { if(ok(x)) ++nr; if(x > n / 10) return; for(int c = 0; c < 10; ++c) if(x * 10 + c <= n && ok(x * 10 + c)) gen(x * 10 + c, n); } int query(int n) { if(n <= 1) return 0; nr = 0; gen(0, n); return nr; } int main() { ios_base::sync_with_stdio(false); int a, b, i, j; for(i = 1; i < VMAX; ++i) isPrime[i] = i % 2; primes.push_back(2); isPrime[2] = true; isPrime[1] = false; isPrime[0] = false; for(i = 3; i < VMAX; i += 2) { if(!isPrime[i]) continue; primes.push_back(i); if(i > 10000) continue; for(j = i * i; j < VMAX; j += i + i) isPrime[j] = false; } cin >> a >> b; cout << query(b) - query(a - 1) << '\n'; return 0; }