from math import floor,ceil

def f(b,n):

    if n < b:

        return n

    else:

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

n = int(eval(input()))

s = int(eval(input()))



for b in range(2,320000):

    if f(b,n)==s:

        print(b)

        exit()



if n==s:

    print((s+1))

    exit()



d = {}

for i in range(1,ceil(n**0.5)):

    m = f(n//i,n)

    if n//i==n//(i+1):

        d[i] = (m,m)

    else:

        M = f(n//(i+1)+1,n)

        d[i] = (m,M)



for i in range(ceil(n**0.5)-1,0,-1):

    m,M = d[i]

    if m<=s<=M and (s-m)%i==0:

        print((n//i-(s-m)//i))

        exit()



print((-1))
def f(b,n):

    if n < b:

        return n

    else:

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



def get_divisor(n):

    res = []

    for i in range(1,int(n**0.5)+1):

        if n%i==0:

            res.append(i)

            res.append(n//i)

    res = list(set(res))

    res.sort()

    return res



n = int(eval(input()))

s = int(eval(input()))

ans = -1



for b in range(2,int(n**0.5)+1):

    if f(b,n)==s:

        ans = b

        break



if ans==-1 and n>=s:

    div = get_divisor(n-s)

    div.append(n)

    for b in div:

        b += 1

        if f(b,n)==s:

            ans = b

            break



print(ans)
from math import floor, ceil


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


n = int(eval(input()))
s = int(eval(input()))
for b in range(2, 320000):
    if f(b, n) == s:
        print(b)
        exit()
if n == s:
    print((s + 1))
    exit()
d = {}
for i in range(1, ceil(n**0.5)):
    m = f(n // i, n)
    if n // i == n // (i + 1):
        d[i] = (m, m)
    else:
        M = f(n // (i + 1) + 1, n)
        d[i] = (m, M)
for i in range(ceil(n**0.5) - 1, 0, -1):
    m, M = d[i]
    if m <= s <= M and (s - m) % i == 0:
        print((n // i - (s - m) // i))
        exit()
print((-1))

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


def get_divisor(n):
    res = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            res.append(i)
            res.append(n // i)
    res = list(set(res))
    res.sort()
    return res


n = int(eval(input()))
s = int(eval(input()))
ans = -1
for b in range(2, int(n**0.5) + 1):
    if f(b, n) == s:
        ans = b
        break
if ans == -1 and n >= s:
    div = get_divisor(n - s)
    div.append(n)
    for b in div:
        b += 1
        if f(b, n) == s:
            ans = b
            break
print(ans)

-from math import floor, ceil - - -        return f(b, floor(n / b)) + n % b +        return f(b, n // b) + n % b + + +def get_divisor(n): +    res = [] +    for i in range(1, int(n**0.5) + 1): +        if n % i == 0: +            res.append(i) +            res.append(n // i) +    res = list(set(res)) +    res.sort() +    return res -for b in range(2, 320000): +ans = -1 +for b in range(2, int(n**0.5) + 1): -        print(b) -        exit() -if n == s: -    print((s + 1)) -    exit() -d = {} -for i in range(1, ceil(n**0.5)): -    m = f(n // i, n) -    if n // i == n // (i + 1): -        d[i] = (m, m) -    else: -        M = f(n // (i + 1) + 1, n) -        d[i] = (m, M) -for i in range(ceil(n**0.5) - 1, 0, -1): -    m, M = d[i] -    if m <= s <= M and (s - m) % i == 0: -        print((n // i - (s - m) // i)) -        exit() -print((-1)) +        ans = b +        break +if ans == -1 and n >= s: +    div = get_divisor(n - s) +    div.append(n) +    for b in div: +        b += 1 +        if f(b, n) == s: +            ans = b +            break +print(ans)