Description

Tidy Numbers

Design a hardware module that finds the largest "tidy" number ≤ N. A tidy number has non-decreasing digits when read left to right. The module should process the input number digit by digit and output the largest tidy number not exceeding the input.

Example: For N=1000, output 999. For N=123, output 123 (already tidy). For N=132, output 129.

Source: Google Code Jam 2017 Qualification Round - Problem B: Tidy Numbers

Input

n

64-bit Unsigned Integer
Input number to find largest tidy ≤ N.

Output

64-bit Unsigned Integer
Largest tidy number ≤ N.