import itertools
x=10
target=100000
d=[0]*target
n=2
while n < target:
for i in range(n+n,target,n):
d[i]+=1
n=n+1
while n < target and d[n]>0:
n+=1
p=[i for i in range(2,target) if d[i]==0]
def isPrime(n):
i=0
while i < len(p) and p[i]*p[i] < n and n%p[i]:
i+=1
return i>=len(p) or n%p[i]
q=[[],[],[],[],[],[],[],[],[],[]]
d=set("0123456789")
n=1
while d:
r=set([])
for i in d:
j=int(i)
t=0
while t < 10**n:
s=str(t).zfill(n)
for pos in itertools.combinations(range(x),n):
z=""
v=0
for u in range(x):
if v < n and u==pos[v]:
z+=s[v]
v+=1
else:
z+=i
y=int(z)
if y>10**(x-1) and isPrime(y):
q[j].append(y)
r.add(i)
t+=1
d=d-r
n+=1
print sum(sum(i) for i in q)