from collections import deque, defaultdict, Counter from bisect import bisect_left, bisect_right, insort from itertools import permutations, combinations, groupby from heapq import heappop, heappush import math, sys input = lambda: sys.stdin.readline().rstrip("\r\n") def printl(li, sep=" "): print(sep.join(map(str, li))) def yn(flag): print(Yes if flag else No) _int = lambda x: int(x)-1 MOD = 998244353 #10**9+7 INF = 1<<60 Yes, No = "Yes", "No" nums = list(map(int, input())) N = len(nums) M = 2*2 dp = [[-1]*M for _ in range(N+1)] def ind(small, d): return small*2+d dp[0][ind(0, 0)] = 0 for i in range(N): num = nums[i] for d in range(2): for small in range(2): key = ind(small, d) if dp[i][key] < 0: continue for nd in [4, 5]: if not small and nd > num: break nsmall = small | (1 if nd < num else 0) nkey = ind(nsmall, nd-4) dp[i+1][nkey] = key ans = 0 for d in reversed(range(2)): for small in range(2): if dp[N][ind(small, d)] < 0: continue i = ind(small, d) cur = N li = [] for _ in range(N): li.append(str(i%2+4)) i = dp[cur][i] cur -= 1 li.reverse() ans = max(ans, int("".join(li))) ans = max(ans, int("5"*(N-1))) print(ans)