n = int(input()) for i in range(n-1, 0, -1): if i % 3 != 0: if "3" not in str(i): print(i) break