Each digit at most twice

Find the largest integer L not greater than U whose decimal digits each appear at most twice.

Medium5BacktrackingGreedyStringInterviewNo attempts yetTime limit3sMemory limit256 MB

Problem

Write a positive integer in decimal without leading zeros. The number satisfies the rule when no digit appears three or more times. For example 2015201520152015 satisfies the rule, and 10001000 does not because the digit 00 appears three times.

Given a positive integer UU, find the largest integer LL with LUL \le U that satisfies the rule. Every one-digit number satisfies the rule, so such an LL always exists.

Input

The first line contains an integer UU (1U10181 \le U \le 10^{18}).

Output

Print the largest integer not greater than UU whose decimal representation has no digit appearing three or more times.