public class Euler156 {
    static long countDigitUpto(long n, int d) {
        n++;
        long result = 0, p10 = 1;
        while (p10 < n) {
            int cur = (int) ((n / p10) % 10);
            result += (n / p10 / 10) * p10;
            if (cur == d)
                result += n % p10;
            else if (cur > d)
                result += p10;
            p10 *= 10;
        }
        return result;
    }

    static long search(int d, long start, long span, long limit) {
        if (start > limit)
            return 0;
        long right = Math.min(limit, start + span);
        long lo = countDigitUpto(start, d), hi = countDigitUpto(right, d);
        if (hi < start || lo > right)
            return 0;
        if (span == 1)
            return lo == start ? start : 0;
        long step = span / 10, acc = 0;
        for (int k = 0; k < 10; k++)
            acc += search(d, start + k * step, step, limit);
        return acc;
    }

    public static void main(String[] args) {
        long limit = 100000000000L, rootSpan = 1;
        while (rootSpan < limit)
            rootSpan *= 10;
        long total = 0;
        for (int d = 1; d <= 9; d++)
            total += search(d, 0, rootSpan, limit);
        System.out.println(total);
    }
}
