for i in range(int(input()) ,0,-1): if '3' in str(i): continue elif i % 3 == 0: continue print(i) break