import std; void main () { auto N = readln.chomp.map!(a => a - '0').array; const int L = N.length.to!int; const dig = [4, 5]; bool[Tuple!(int, bool)] memo; bool dp (int pos, bool less) { auto key = tuple(pos, less); if (pos == L) { return true; } bool ret = false; foreach (nex; dig) { if (!less && N[pos] < nex) { continue; } bool nless = less || nex < N[pos]; ret = ret || dp(pos + 1, nless); } return memo[key] = ret; } bool less = false; auto ans = new int[](0); bool fi = false; foreach (i; 0 .. L) { foreach_reverse (nex; dig) { if (!less && N[i] < nex) { continue; } bool nless = less || nex < N[i]; if (dp(i + 1, nless)) { if (i == 0) { fi = true; } ans ~= nex; less = nless; break; } } } if (fi) { writefln("%(%s%)", ans); } else { writeln(replicate("5", L - 1)); } } void read (T...) (string S, ref T args) { import std.conv : to; import std.array : split; auto buf = S.split; foreach (i, ref arg; args) { arg = buf[i].to!(typeof(arg)); } }