def mkp(x): P = [2] p = [2] n = 3 while n < x: for i in p: if n % i == 0: break else: P.append(n) n += 2 while n > p[-1] ** 2: p.append(P[len(p)]) return P def re(x, p): za = [0, 1, 1] zb = [1, 1, 0] a = p b = x n = 3 while b != 0: n += 1 q = a / b za[n % 3] = za[(n - 2) % 3] - q * za[(n - 1) % 3] zb[n % 3] = zb[(n - 2) % 3] - q * zb[(n - 1) % 3] a, b = b, a % b return (zb[(n - 1) % 3] + p) % p def S(p1, p2): tmp = 10 ** (len(str(p1))) return (p2 - p1) * re(tmp, p2) % p2 p = mkp(1000004) print p[-1] total = 0 for i in xrange(3, len(p)): total += int(str(S(p[i - 1], p[i])) + str(p[i - 1])) print total