atcoder#ABC192D. [ABC192D] Base n

[ABC192D] Base n

Score : 400400 points

Problem Statement

Given are a string XX consisting of 0 through 9, and an integer MM.

Let dd be the greatest digit in XX.

How many different integers not greater than MM can be obtained by choosing an integer nn not less than d+1d+1 and seeing XX as a base-nn number?

Constraints

  • XX consists of 0 through 9.
  • The length of XX is between 11 and 6060 (inclusive).
  • XX does not begin with a 0.
  • 1M10181 \leq M \leq 10^{18}

Input

Input is given from Standard Input in the following format:

XX

MM

Output

Print the answer.

22
10
2

The greatest digit in XX is 2.

  • By seeing XX as a base-33 number, we get 88.
  • By seeing XX as a base-44 number, we get 1010.

These two values are the only ones that we can obtain and are not greater than 1010.

999
1500
3

The greatest digit in XX is 9.

  • By seeing XX as a base-1010 number, we get 999999.
  • By seeing XX as a base-1111 number, we get 11971197.
  • By seeing XX as a base-1212 number, we get 14131413.

These three values are the only ones that we can obtain and are not greater than 15001500.

100000000000000000000000000000000000000000000000000000000000
1000000000000000000
1

By seeing XX as a base-22 number, we get 576460752303423488576460752303423488, which is the only value that we can obtain and are not greater than 10000000000000000001000000000000000000.