Base Sums

Given n, a, b, find the smallest m greater than n whose digit sum in base a equals its digit sum in base b.

Hard8Number theoryMathBrute forceBinary searchNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given three integers nn, aa, and bb. Find the smallest integer mm greater than nn whose digit sum in base aa equals its digit sum in base bb.

Input

The input is a single test case. The first line holds three integers nn, aa, and bb, separated by spaces, with 0n10160 \le n \le 10^{16} and 2a<b362 \le a < b \le 36. All three are given in base 10.

Output

Print in base 10, on one line, the smallest integer mm greater than nn whose digit sum in base aa equals its digit sum in base bb. The value of mm can be larger than 101610^{16}.