N = int(input()) for i in range(N, 0, -1): if i % 3 == 0 or str(i).count("3") > 0: continue print(i) exit()