def A(x): if x % 2 == 0 or x % 5 == 0: return -1 n = 0 num = 0 while 1: n = (n * 10 + 1) % x num += 1 if n == 0: return num P = [2] p = [2] n = 3 out = [] while len(out) < 25: for i in p: if n % i == 0: tmp = A(n) if tmp > 0: if (n - 1) % A(n) == 0: out.append(n) break else: P.append(n) n += 2 while n > p[-1] ** 2: p.append(P[len(p)]) print sum(out)