import java.math.BigInteger;

public class Euler284 {

    static final int kBase = 14;

    static String toBase14(long value) {
        char[] digits = "0123456789abcd".toCharArray();
        if (value == 0)
            return "0";
        StringBuilder sb = new StringBuilder();
        while (value > 0) {
            sb.append(digits[(int) (value % 14)]);
            value /= 14;
        }
        return sb.reverse().toString();
    }

    static int mod14(BigInteger v) {
        BigInteger r = v.remainder(BigInteger.valueOf(kBase));
        if (r.signum() < 0) {
            r = r.add(BigInteger.valueOf(kBase));
        }
        return r.intValue();
    }

    static class SequenceState {
        BigInteger x;
        BigInteger f;
        BigInteger mod;
        int leadingDigit;
        long digitSum;

        SequenceState(int root) {
            this.x = BigInteger.valueOf(root);
            this.mod = BigInteger.valueOf(kBase);
            this.f = this.x.multiply(this.x).subtract(this.x).divide(BigInteger.valueOf(kBase));
            this.leadingDigit = root;
            this.digitSum = root;
        }

        void step(int[] invMod14) {
            BigInteger xOld = x;
            BigInteger modOld = mod;

            int a = mod14(xOld.multiply(BigInteger.TWO).subtract(BigInteger.ONE));
            int inv = invMod14[a];
            int fMod = mod14(f);
            int t = ((kBase - fMod) % kBase * inv) % kBase;

            x = xOld.add(modOld.multiply(BigInteger.valueOf(t)));
            BigInteger term2 = xOld.multiply(BigInteger.TWO).subtract(BigInteger.ONE).multiply(BigInteger.valueOf(t));
            BigInteger term3 = modOld.multiply(BigInteger.valueOf((long) t * t));
            f = f.add(term2).add(term3).divide(BigInteger.valueOf(kBase));
            mod = modOld.multiply(BigInteger.valueOf(kBase));

            leadingDigit = t;
            digitSum += t;
        }
    }

    static long solveSumOfDigitSums(int nMax) {
        int[] invMod14 = new int[kBase];
        for (int i = 0; i < kBase; i++)
            invMod14[i] = -1;
        for (int a = 1; a < kBase; a++) {
            for (int b = 1; b < kBase; b++) {
                if ((a * b) % kBase == 1) {
                    invMod14[a] = b;
                    break;
                }
            }
        }

        SequenceState seq7 = new SequenceState(7);
        SequenceState seq8 = new SequenceState(8);

        long total = 1;

        for (int n = 1; n <= nMax; n++) {
            if (seq7.leadingDigit != 0) {
                total += seq7.digitSum;
            }
            if (seq8.leadingDigit != 0) {
                total += seq8.digitSum;
            }

            if (n == nMax)
                break;

            seq7.step(invMod14);
            seq8.step(invMod14);
        }

        return total;
    }

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