import java.util.*;

public class Euler293 {
    static long modPow(long base, long exp, long mod) {
        long result = 1L % mod;
        long cur = base % mod;
        while (exp > 0) {
            if ((exp & 1) != 0) {
                // To avoid overflow, use BigInteger for multiplication modulo
                result = java.math.BigInteger.valueOf(result)
                        .multiply(java.math.BigInteger.valueOf(cur))
                        .mod(java.math.BigInteger.valueOf(mod)).longValue();
            }
            cur = java.math.BigInteger.valueOf(cur)
                    .multiply(java.math.BigInteger.valueOf(cur))
                    .mod(java.math.BigInteger.valueOf(mod)).longValue();
            exp >>= 1;
        }
        return result;
    }

    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 & 1) == 0) {
            d >>= 1;
            ++s;
        }

        long[] bases = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
        for (long a : bases) {
            if (a % n == 0)
                continue;
            long x = modPow(a, d, n);
            if (x == 1 || x == n - 1)
                continue;
            boolean witness = 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) {
                    witness = false;
                    break;
                }
            }
            if (witness)
                return false;
        }
        return true;
    }

    static void generateAdmissible(int[] primes, int idx, long current, long limit, Set<Long> out) {
        if (idx >= primes.length)
            return;
        long p = primes[idx];
        long value = current;
        while (value <= (limit - 1) / p) {
            value *= p;
            out.add(value);
            generateAdmissible(primes, idx + 1, value, limit, out);
        }
    }

    static int pseudoFortunate(long n) {
        for (int m = 3;; m += 2) {
            if (isPrime(n + m)) {
                return m;
            }
        }
    }

    public static String solve() {
        long limit = 1000000000L;
        Set<Long> admissible = new HashSet<>();
        int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
        generateAdmissible(primes, 0, 1L, limit, admissible);

        Set<Integer> pseudoValues = new HashSet<>();
        for (long n : admissible) {
            pseudoValues.add(pseudoFortunate(n));
        }

        long sum = 0;
        for (int m : pseudoValues) {
            sum += m;
        }
        return String.valueOf(sum);
    }

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