Under construction

Still being built. Things may change

Numbers

Interview

Time limit1sMemory limit1024 MB

Summary
Given x and a flag k, find the smallest y at least x whose digits are all equal, or all equal except one digit when k is 1.
Level

Medium4 of 10

Topics
Implementation, Brute force, Math
Solved
No attempts yet

Problem

Darina likes numbers whose digits are all the same. For example, she likes 777777 or 55555555, but she does not like 12341234 at all. When she is in a good mood, she also likes numbers in which all digits except one are the same, such as 7772777727.

Darina has a number xx. She wants to find the smallest integer y≥xy \ge x that she likes, meaning either all of its digits are the same, or only one of its digits is different from the others.

Write a program numbers that, given an integer xx and whether Darina is in a good mood, finds the smallest integer y≥xy \ge x that Darina likes.

Input

The first line of standard input contains one integer xx. The second line contains one integer kk, which is 00 or 11. The value k=1k = 1 means Darina is in a good mood, and k=0k = 0 means she is not.

Output

The first line of standard output contains one integer yy that satisfies the following conditions:

  • y≥xy \ge x.
  • If k=0k = 0, all digits of yy are the same.
  • If k=1k = 1, all digits of yy are the same, except possibly one digit.

Constraints

100≤x≤1017100 \le x \le 10^{17}.

Examples2

  1. Example 1

    Input
    700
    0
    
    Expected output
    777
    
  2. Example 2

    Input
    700
    1
    
    Expected output
    700