import java.util.*;

public class Euler358 {
    static long modPow(long base, long exp, long mod) {
        long result = 1 % mod;
        base %= mod;
        while (exp > 0) {
            if ((exp & 1) != 0) {
                result = (long) (((java.math.BigInteger.valueOf(result)).multiply(java.math.BigInteger.valueOf(base)))
                        .remainder(java.math.BigInteger.valueOf(mod)).longValue());
            }
            base = (long) (((java.math.BigInteger.valueOf(base)).multiply(java.math.BigInteger.valueOf(base)))
                    .remainder(java.math.BigInteger.valueOf(mod)).longValue());
            exp >>= 1;
        }
        return result;
    }

    static boolean isPrime(long n) {
        if (n < 2)
            return false;
        if (n % 2 == 0)
            return n == 2;
        for (long d = 3; d * d <= n; d += 2) {
            if (n % d == 0)
                return false;
        }
        return true;
    }

    static List<Long> primeFactorsDistinct(long n) {
        List<Long> factors = new ArrayList<>();
        if (n % 2 == 0) {
            factors.add(2L);
            while (n % 2 == 0)
                n /= 2;
        }
        for (long d = 3; d * d <= n; d += 2) {
            if (n % d == 0) {
                factors.add(d);
                while (n % d == 0)
                    n /= d;
            }
        }
        if (n > 1)
            factors.add(n);
        return factors;
    }

    static boolean isFullReptendPrimeBase10(long p) {
        if (!isPrime(p) || p == 2 || p == 5)
            return false;
        long phi = p - 1;
        for (long q : primeFactorsDistinct(phi)) {
            if (modPow(10, phi / q, p) == 1)
                return false;
        }
        return true;
    }

    public static String solve() {
        long low = 100000000000L / 138 + 1;
        long high = 100000000000L / 137;
        long mod = 100000L;
        long residue = 9891L;

        long start = low;
        long rem = start % mod;
        if (rem <= residue) {
            start += residue - rem;
        } else {
            start += mod - (rem - residue);
        }

        long found = 0;
        for (long p = start; p <= high; p += mod) {
            if (100000000000L / p != 137)
                continue;
            if (!isFullReptendPrimeBase10(p))
                continue;
            if ((p * 56789L + 1) % mod != 0)
                continue;
            found = p;
            break;
        }

        return String.valueOf((9 * (found - 1)) / 2);
    }

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