Under construction

Still being built. Things may change

Numbers

Interview

Time limit1sMemory limit512 MB

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

Medium5 of 10

Topics
Brute force, Implementation, Greedy
Solved
No attempts yet

Problem

Anya likes numbers made of identical digits. She likes 777 or 5555, but she does not like 1234 at all.

When Anya is in a good mood, she still likes all numbers made of identical digits, and she also likes numbers in which all digits except one are the same, such as 77727.

Anya has an integer xx. She wants to find the smallest integer y≥xy \ge x that she will like.

Given an integer xx and whether Anya is in a good mood, write a program that finds the smallest integer y≥xy \ge x that Anya likes.

Input

The first line contains an integer xx (1≤x≤10171 \le x \le 10^{17}). This number does not fit in a 32-bit type, so use a 64-bit type such as long long in C++ or int64 in Pascal.

The second line contains kk, which is 0 or 1. k=1k = 1 means Anya is in a good mood, and k=0k = 0 means she is not.

Output

Print one integer yy.

It must satisfy:

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

Examples2

  1. Example 1

    Input
    700
    0
    
    Expected output
    777
    
  2. Example 2

    Input
    700
    1
    
    Expected output
    700