#arc060b. [arc060_b]Digit Sum

[arc060_b]Digit Sum

Problem Statement

For integers b(bgeq2)b (b \\geq 2) and n(ngeq1)n (n \\geq 1), let the function f(b,n)f(b,n) be defined as follows:

  • f(b,n)=nf(b,n) = n, when n<bn < b
  • $f(b,n) = f(b,\\,{\\rm floor}(n / b)) + (n \\ {\\rm mod} \\ b)$, when ngeqbn \\geq b

Here, rmfloor(n/b){\\rm floor}(n / b) denotes the largest integer not exceeding n/bn / b, and nrmmodbn \\ {\\rm mod} \\ b denotes the remainder of nn divided by bb.

Less formally, f(b,n)f(b,n) is equal to the sum of the digits of nn written in base bb. For example, the following hold:

  • f(10,,87654)=8+7+6+5+4=30f(10,\\,87654)=8+7+6+5+4=30
  • f(100,,87654)=8+76+54=138f(100,\\,87654)=8+76+54=138

You are given integers nn and ss. Determine if there exists an integer b(bgeq2)b (b \\geq 2) such that f(b,n)=sf(b,n)=s. If the answer is positive, also find the smallest such bb.

Constraints

  • 1leqnleq10111 \\leq n \\leq 10^{11}
  • 1leqsleq10111 \\leq s \\leq 10^{11}
  • n,,sn,\\,s are integers.

Input

The input is given from Standard Input in the following format:

nn ss

Output

If there exists an integer b(bgeq2)b (b \\geq 2) such that f(b,n)=sf(b,n)=s, print the smallest such bb. If such bb 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