input = 5000

table = [i for i in range(1, input+1)]

for i in range(1, input+1):
    if i%5 != 0 and i%7 != 0:
        table.remove(i)
        continue
    if (i//10)%10>i%10:
        table.remove(i)
        continue

to_delete = [table[-1], ]
for i in range(len(table)-1):
    if table[i+1]%10>=5:
        to_delete.append(table[i])
for i in to_delete:
    table.remove(i)

to_delete = list()
for i in range(len(table)):
    if ((table[i]//10)%10)%2 == 0:
        to_delete.append(table[i])
for i in to_delete:
    table.remove(i)

n = len(table)//2
print(table[n-1], table[n])

