#arc060b. [arc060_b]Digit Sum
[arc060_b]Digit Sum
Problem Statement
For integers and , let the function be defined as follows:
- , when
- $f(b,n) = f(b,\\,{\\rm floor}(n / b)) + (n \\ {\\rm mod} \\ b)$, when
Here, denotes the largest integer not exceeding , and denotes the remainder of divided by .
Less formally, is equal to the sum of the digits of written in base . For example, the following hold:
You are given integers and . Determine if there exists an integer such that . If the answer is positive, also find the smallest such .
Constraints
- are integers.
Input
The input is given from Standard Input in the following format:
Output
If there exists an integer such that , print the smallest such . If such does not exist, print -1
instead.
Sample Input 1
87654
30
Sample Output 1
10
Sample Input 2
87654
138
Sample Output 2
100
Sample Input 3
87654
45678
Sample Output 3
-1
Sample Input 4
31415926535
1
Sample Output 4
31415926535
Sample Input 5
1
31415926535
Sample Output 5
-1