N = int(input()) for i in range(N, 0, -1): if str(i).count("3") == 0 and i % 3 != 0: print(i) break