MAX_N = 10000000+1

def make_sosu_tbl():
    for i in range(2, MAX_N):
        if sos[i]:
            for j in range(i+i, MAX_N+1, i):
                sos[j] = 0
        if i**2 >= MAX_N:break

sos = [1] * (MAX_N+1)
make_sosu_tbl()
while 1:
#    MAX_N = int(input())
#    if not MAX_N: break
#    
    n = int(input())
    for i in reversed(range(13,n+1)):
        if sos[i] and sos[i-2] and sos[i-6] and sos[i-8]:
            print(i)
            break