

Time Limit: 2 sec / Memory Limit: 256 MiB
配点 : 点
問題文
以上の整数 および 以上の整数 に対し、関数 を次のように定義します。
- のとき
- のとき
ここで、 は を超えない最大の整数を、 は を で割った余りを表します。
直感的に言えば、 は、 を 進表記したときの各桁の和となります。 例えば、
などとなります。
整数 と が与えられます。 を満たすような 以上の整数 が存在するか判定してください。 さらに、そのような が存在するならば、その最小値を求めてください。
制約
- はいずれも整数である
入力
入力は以下の形式で標準入力から与えられる。
出力
を満たす 以上の整数 が存在するならば、そのような の最小値を出力せよ。
そのような が存在しないならば、代わりに -1
を出力せよ。
入力例 1Copy
87654 30
出力例 1Copy
10
入力例 2Copy
87654 138
出力例 2Copy
100
入力例 3Copy
87654 45678
出力例 3Copy
-1
入力例 4Copy
31415926535 1
出力例 4Copy
31415926535
入力例 5Copy
1 31415926535
出力例 5Copy
-1
Score : points
Problem Statement
For integers and , let the function be defined as follows:
- , when
- , 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 1Copy
87654 30
Sample Output 1Copy
10
Sample Input 2Copy
87654 138
Sample Output 2Copy
100
Sample Input 3Copy
87654 45678
Sample Output 3Copy
-1
Sample Input 4Copy
31415926535 1
Sample Output 4Copy
31415926535
Sample Input 5Copy
1 31415926535
Sample Output 5Copy
-1