import java.math.BigInteger;

public class Euler700 {
    static final long kA = 1504170715041707L;
    static final long kM = 4503599627370517L;
    static final long kForwardStop = 20000000L;

    static long modInverse(long a, long mod) {
        long t = 0;
        long newT = 1;
        long r = mod;
        long newR = a;

        while (newR != 0) {
            long q = r / newR;

            long tNext = t - q * newT;
            t = newT;
            newT = tNext;

            long rNext = r - q * newR;
            r = newR;
            newR = rNext;
        }

        if (t < 0) {
            t += mod;
        }
        return t;
    }

    static long modMul(long a, long b, long mod) {
        return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).mod(BigInteger.valueOf(mod)).longValue();
    }

    public static String solve() {
        long sum = 0;

        long term = kA;
        long bestCoin = kA;
        sum += bestCoin;

        while (bestCoin > kForwardStop) {
            term += kA;
            if (term >= kM) {
                term -= kM;
            }
            if (term < bestCoin) {
                bestCoin = term;
                sum += bestCoin;
            }
        }

        long inv = modInverse(kA, kM);

        long bestIndex = kM + 1;
        for (long value = 1; value < bestCoin; ++value) {
            long idx = modMul(inv, value, kM);
            if (idx < bestIndex) {
                bestIndex = idx;
                sum += value;
            }
        }

        return Long.toString(sum);
    }

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