This page is still under construction.

Parts of this page are still being built. What you see may change.

Changing Digits

Time limit1sMemory limit1024 MB

Summary
Find the largest number reachable from N by changing one digit at a time so that each new value's remainder modulo M strictly increases.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Math
Solved
No attempts yet

Statement

Dovydas told Justas two natural numbers NN and MM. Justas's task is to turn NN into the largest possible number. He may do this by changing NN one digit at a time, so that after each change the remainder of the number divided by MM strictly increases. When changing a digit, the leading digit may not be changed to 00.

For example, if N=1399N = 1399 and M=11M = 11, then the remainder of NN divided by MM is initially 1399 mod 11=21399 \bmod 11 = 2. If Justas first changes the last digit of NN, he may choose it among 0,1,…,60, 1, \dots, 6, because these options increase the remainder:

N1390139113921393139413951396139713981399
remainder45678910012

Suppose he chooses 33, so NN is now 13931393. If Justas now changes the leading digit, the only possible value is 99 (note that the leading digit may not be changed to 00):

N139323933393439353936393739383939393
remainder7654321010

The number 93939393 can no longer be changed, because the largest possible remainder modulo 1111 has already been reached (9393 mod 11=109393 \bmod 11 = 10). This would be Justas's result.

However, this is not the best possible result. The best result is 98999899, which can be reached, for example, with the following steps:

N1399 → 1899 → 9899
remainder2 → 7 → 10

Determine the largest number Justas can reach.

Input

A single line contains the two integers NN and MM.

Output

Output the largest number Justas can reach.

Constraints

  • 1≤N<10171 \le N < 10^{17}
  • 1≤M≤1091 \le M \le 10^9

Examples3

  1. Example 1

    Input
    1399 11
    
    Expected output
    9899
    
  2. Example 2

    Input
    123 10
    
    Expected output
    129
    
  3. Example 3

    Input
    89 7
    
    Expected output
    89