Олимпиадный тренинг

Задача 38646. The sum of the digits of a number


For integers b ( b >= 2 ) and n ( n >= 1 ) let the function f(b, n) be defined as follows:
\(f (b, n) = n when\ n < b \\ f (b, n) = f (b, floor (n / b)) + (n \ mod \ b) when \ n >= b\)

Here
floor(n / b) denotes the largest integer not greater than n / b;
n mod b denotes the remainder of n divided by b.

Less formally, f(b, n) is equal to the sum of the n digits in base b. For example, the following is true:
\(f (10.87654) = 8 + 7 + 6 + 5 + 4 = 30\\ f (100.87654) = 8 + 76 + 54 = 138\)

You are given the integers n and s. Determine if there is an integer b (b >= 2) such that f(b, n) = s. If the answer is yes, find the smallest of these b.


Input
The first line contains an integer n (1 <= n <= 1011). The second line contains an integer (1 <= s <= 1011).

Imprint
Output the answer to the problem. If there is no answer, then print -1.
 

 

Examples
# Input Output
1 87654
30
10
2 87654
138
100
3 87654
45678
-1
4 31415926535
1
31415926535
5 1
31415926535
-1