n = input() i = int(n) while True: dig = list(str(i)) if "3" in dig or i % 3 == 0: i -= 1 else: print(i) break