import math

# https://projecteuler.net/problem=35

#TOTAL=100
TOTAL=10**6

prime = [True for i in range(TOTAL + 1)] 

# https://www.geeksforgeeks.org/python-program-for-sieve-of-eratosthenes/
def SieveOfEratosthenes_init():
    p = 2
    while (p * p <= TOTAL):
        # If prime[p] is not changed, then it is a prime 
        if prime[p]:
            # Update all multiples of p 
            for i in range(p * 2, TOTAL + 1, p): 
                prime[i] = False
        p += 1
    prime[0]= False
    prime[1]= False

def all_rotations(n):
    total_numbers=math.ceil(math.log(n, 10))
    if total_numbers==0:
        return [n]
    rt=[]
    for i in range (total_numbers):
        n=int(n/10) + int((n%10)) * 10**(total_numbers-1)
        rt.append (n)
    return rt

def all_rotations_are_prime(n):
    rots=all_rotations(n)
    for rot in rots:
        if prime[rot]==False:
            return False
    return True

#print (all_rotations(1))
#print (all_rotations(12))
#print (all_rotations(123))
#print (all_rotations(1234))

SieveOfEratosthenes_init()
#print (prime)

cnt=0
for i in range(TOTAL):
    if prime[i]:
        #print (i)
        if (all_rotations_are_prime(i)):
            print (i)
            cnt=cnt+1
print ("cnt", cnt)

