import java.math.BigInteger;

public class Euler132 {
    static boolean isPrime(int n) {
        if (n < 2)
            return false;
        if (n % 2 == 0)
            return n == 2;
        for (int p = 3; (long) p * p <= n; p += 2)
            if (n % p == 0)
                return false;
        return true;
    }

    public static void main(String[] args) {
        long exp = 1000000000L;
        int found = 0;
        long sum = 0;
        BigInteger TEN = BigInteger.TEN, EXP = BigInteger.valueOf(exp);
        for (int p = 2; found < 40; p++) {
            if (!isPrime(p))
                continue;
            if (p == 2 || p == 5)
                continue;
            BigInteger mod = BigInteger.valueOf(9L * p);
            if (TEN.modPow(EXP, mod).equals(BigInteger.ONE)) {
                found++;
                sum += p;
            }
        }
        System.out.println(sum);
    }
}
