import java.util.ArrayList;

public class Euler808 {

    static long mulMod(long a, long b, long mod) {
        long x0 = a & 0xFFFFFFL;
        long x1 = (a >>> 24) & 0xFFFFFFL;
        long y0 = b & 0xFFFFFFL;
        long y1 = (b >>> 24) & 0xFFFFFFL;

        long p0 = x0 * y0;
        long p1 = x1 * y0 + x0 * y1;

        return (p0 + (p1 << 24)) % mod;
    }

    static long powMod(long a, long e, long mod) {
        long r = 1 % mod;
        a %= mod;
        while (e > 0) {
            if ((e & 1L) == 1L) {
                // simple modular product since mod is up to ~ 10^16, we can use
                // Math.multiplyHigh, but since values are small, BigInteger or custom is better
                // if overflow happens.
                // Wait, java 9+ has Math.multiplyHigh, but here, mod is at most sqrt(10^16)^2?
                // Wait.
                // For Miller-Rabin, n can be up to 10^16.
                // We can just use BigInteger for powMod to be safe if mod is large.
                r = java.math.BigInteger.valueOf(r).multiply(java.math.BigInteger.valueOf(a))
                        .mod(java.math.BigInteger.valueOf(mod)).longValue();
            }
            a = java.math.BigInteger.valueOf(a).multiply(java.math.BigInteger.valueOf(a))
                    .mod(java.math.BigInteger.valueOf(mod)).longValue();
            e >>= 1L;
        }
        return r;
    }

    static boolean isPrime(long n) {
        if (n < 2)
            return false;
        long[] smallPrimes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
        for (long p : smallPrimes) {
            if (n == p)
                return true;
            if (n % p == 0)
                return false;
        }

        long d = n - 1;
        int s = 0;
        while ((d & 1L) == 0L) {
            d >>= 1L;
            s++;
        }

        long[] witnesses = { 2L, 325L, 9375L, 28178L, 450775L, 9780504L, 1795265022L };
        for (long a : witnesses) {
            if (a % n == 0)
                continue;
            long x = powMod(a, d, n);
            if (x == 1 || x == n - 1)
                continue;
            boolean comp = true;
            for (int r = 1; r < s; r++) {
                x = java.math.BigInteger.valueOf(x).multiply(java.math.BigInteger.valueOf(x))
                        .mod(java.math.BigInteger.valueOf(n)).longValue();
                if (x == n - 1) {
                    comp = false;
                    break;
                }
            }
            if (comp)
                return false;
        }
        return true;
    }

    static ArrayList<Long> sievePrimes(int limit) {
        boolean[] isPrimeVec = new boolean[limit + 1];
        for (int i = 2; i <= limit; i++)
            isPrimeVec[i] = true;

        int r = (int) Math.sqrt(limit);
        for (int p = 2; p <= r; p++) {
            if (isPrimeVec[p]) {
                for (int q = p * p; q <= limit; q += p) {
                    isPrimeVec[q] = false;
                }
            }
        }

        ArrayList<Long> primes = new ArrayList<>();
        for (int i = 2; i <= limit; i++) {
            if (isPrimeVec[i]) {
                primes.add((long) i);
            }
        }
        return primes;
    }

    static long reverseDigits(long x) {
        long r = 0;
        while (x > 0) {
            r = r * 10 + (x % 10);
            x /= 10;
        }
        return r;
    }

    static boolean isPalindrome(long x) {
        return x == reverseDigits(x);
    }

    static long isqrt(long n) {
        if (n < 0)
            return 0;
        long r = (long) Math.sqrt(n);
        while ((r + 1) * (r + 1) <= n && (r + 1) > r) {
            r++;
        }
        while (r * r > n) {
            r--;
        }
        return r;
    }

    static ArrayList<Long> firstReversiblePrimeSquares(int need) {
        for (int limit = 1 << 16;; limit <<= 1) {
            ArrayList<Long> primes = sievePrimes(limit);
            ArrayList<Long> vals = new ArrayList<>();

            for (long p : primes) {
                long sq = p * p;
                if (isPalindrome(sq))
                    continue;
                long rev = reverseDigits(sq);
                long r = isqrt(rev);
                if (r * r == rev && isPrime(r)) {
                    vals.add(sq);
                    if (vals.size() >= need) {
                        return new ArrayList<>(vals.subList(0, need));
                    }
                }
            }
            if (limit >= (1 << 30))
                break;
        }
        return new ArrayList<>();
    }

    public static String solve() {
        ArrayList<Long> vals = firstReversiblePrimeSquares(50);
        long sum = 0;
        for (long v : vals) {
            sum += v;
        }
        return Long.toString(sum);
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}
