from math import floor, sqrt, ceil





def f(b, n):

    if b < 2:

        return 0



    if n < b:

        return n



    return f(b, floor(n / b)) + n % b





n = int(eval(input()))

s = int(eval(input()))



if n == s:

    print((n + 1))

    exit()



for b in range(2, floor(sqrt(n)) + 1):

    if f(b, n) == s:

        print(b)

        exit()



for p in range(1, ceil(sqrt(n)))[::-1]:

    b = (n - s) / p + 1

    if b.is_integer():

        b = int(b)

        if 1 <= p < b and 0 <= s - p < b:

            print(b)

            exit()



print((-1))

from math import floor, sqrt, ceil





def f(b, n):

    if n < b:

        return n



    return f(b, floor(n / b)) + n % b





n = int(eval(input()))

s = int(eval(input()))



if n == s:

    print((n + 1))

    exit()



for b in range(2, floor(sqrt(n)) + 1):

    if f(b, n) == s:

        print(b)

        exit()



for p in range(1, ceil(sqrt(n)))[::-1]:

    b = (n - s) / p + 1

    if b.is_integer():

        b = int(b)

        if 1 <= p < b and 0 <= s - p < b:

            print(b)

            exit()



print((-1))

from math import floor, sqrt, ceil


def f(b, n):
    if b < 2:
        return 0
    if n < b:
        return n
    return f(b, floor(n / b)) + n % b


n = int(eval(input()))
s = int(eval(input()))
if n == s:
    print((n + 1))
    exit()
for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()
for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if 1 <= p < b and 0 <= s - p < b:
            print(b)
            exit()
print((-1))

from math import floor, sqrt, ceil


def f(b, n):
    if n < b:
        return n
    return f(b, floor(n / b)) + n % b


n = int(eval(input()))
s = int(eval(input()))
if n == s:
    print((n + 1))
    exit()
for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()
for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if 1 <= p < b and 0 <= s - p < b:
            print(b)
            exit()
print((-1))

-    if b < 2: -        return 0