#ABC192C. [ABC192C] Kaprekar Number

[ABC192C] Kaprekar Number

Score : 300300 points

Problem Statement

For an integer xx not less than 00, we define g1(x),g2(x),f(x)g_1(x), g_2(x), f(x) as follows:

  • g1(x)=g_1(x)= the integer obtained by rearranging the digits in the decimal notation of xx in descending order
  • g2(x)=g_2(x)= the integer obtained by rearranging the digits in the decimal notation of xx in ascending order
  • f(x)=g1(x)g2(x)f(x)=g_1(x)-g_2(x)

For example, we have g1(314)=431g_1(314)=431, g2(3021)=123g_2(3021)=123, f(271)=721127=594f(271)=721-127=594. Note that the leading zeros are ignored.

Given integers N,KN, K, find aKa_K in the sequence defined by a0=Na_0=N, ai+1=f(ai) (i0)a_{i+1}=f(a_i)\ (i\geq 0).

Constraints

  • 0N1090 \leq N \leq 10^9
  • 0K1050 \leq K \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK

Output

Print aKa_K.

314 2
693

We have:

  • a0=314a_0=314
  • a1=f(314)=431134=297a_1=f(314)=431-134=297
  • a2=f(297)=972279=693a_2=f(297)=972-279=693
1000000000 100
0

We have:

  • a0=1000000000a_0=1000000000
  • a1=f(1000000000)=10000000001=999999999a_1=f(1000000000)=1000000000-1=999999999
  • a2=f(999999999)=999999999999999999=0a_2=f(999999999)=999999999-999999999=0
  • a3=f(0)=00=0a_3=f(0)=0-0=0
  • \vdots
6174 100000
6174