import sys input = sys.stdin.readline LIST=[[[0,1,2,4,5,6],[3,4,5,6]], [[2,5],[1,4]], [[0,2,3,4,6]], [[0,2,3,5,6]], [[1,2,3,5]], [[0,1,3,5,6]], [[0,1,3,4,5,6],[1,3,4,5,6]], [[0,2,5],[0,1,2,5]], [[]], [[0,1,2,3,5,6],[0,1,2,3,5]] ] def calc(S): NOW=[[0]*7] NNOW=[] for i in range(len(S)): s=S[i] now2=[] while NOW: nowl=NOW.pop() for ll in LIST[int(s)]: ni=nowl[:] for x in ll: ni[x]+=1 now2.append(ni[:]) #print(now2) now2.sort() NOW=[] for x in now2: if NOW and NOW[-1]==x: continue if max(x)-min(x)>N//2+1: continue NOW.append(x) #print(s,NOW) if NOW==[]: return len(S)-i for x in NOW: if len(set(x))==1: return -1 return 0 N=int(input()) while True: #print(N) ANS=calc(str(N)) if ANS==-1: print(N) break else: N+=10**ANS