import math

def f(b,n):

    if n<b:

        return n

    else:

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

N=int(eval(input()))

S=int(eval(input()))

if N==S:

    print((N+1))

else:

    for b in range(2,int(math.sqrt(N))+2):

        if f(b,N)==S:

            print(b)

            exit()

    ans=10**13

    for p in range(1,int(math.sqrt(N))+2):

        if (N-S)%p!=0:

            continue

        b=(N-S)//p+1

        if b<=1:

            continue

        if f(b,N)==S:

            ans=b

    if ans==10**13:

        print((-1))

    else:

        print(ans)



import math

n=int(eval(input()))

s=int(eval(input()))



def f(b,n):

    if n<b:

        return n

    else:

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





def abc(N,S):

    if N==S:

        return N+1

    else:

        for b in range(2,int(math.sqrt(N))+2):

            if f(b,N)==S:

                return b

        tmp=-1

        for p in range(1,int(math.sqrt(N))+2):

            if (N-S)%p!=0:

                continue

            b=(N-S)//p+1

            if b<=1:

                continue

            if f(b,N)==S:

                tmp=b

        return tmp

ans=abc(n,s)

print(ans)
import math


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


N = int(eval(input()))
S = int(eval(input()))
if N == S:
    print((N + 1))
else:
    for b in range(2, int(math.sqrt(N)) + 2):
        if f(b, N) == S:
            print(b)
            exit()
    ans = 10**13
    for p in range(1, int(math.sqrt(N)) + 2):
        if (N - S) % p != 0:
            continue
        b = (N - S) // p + 1
        if b <= 1:
            continue
        if f(b, N) == S:
            ans = b
    if ans == 10**13:
        print((-1))
    else:
        print(ans)

import math

n = int(eval(input()))
s = int(eval(input()))


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


def abc(N, S):
    if N == S:
        return N + 1
    else:
        for b in range(2, int(math.sqrt(N)) + 2):
            if f(b, N) == S:
                return b
        tmp = -1
        for p in range(1, int(math.sqrt(N)) + 2):
            if (N - S) % p != 0:
                continue
            b = (N - S) // p + 1
            if b <= 1:
                continue
            if f(b, N) == S:
                tmp = b
        return tmp


ans = abc(n, s)
print(ans)

+ +n = int(eval(input())) +s = int(eval(input())) -N = int(eval(input())) -S = int(eval(input())) -if N == S: -    print((N + 1)) -else: -    for b in range(2, int(math.sqrt(N)) + 2): -        if f(b, N) == S: -            print(b) -            exit() -    ans = 10**13 -    for p in range(1, int(math.sqrt(N)) + 2): -        if (N - S) % p != 0: -            continue -        b = (N - S) // p + 1 -        if b <= 1: -            continue -        if f(b, N) == S: -            ans = b -    if ans == 10**13: -        print((-1)) +def abc(N, S): +    if N == S: +        return N + 1 -        print(ans) +        for b in range(2, int(math.sqrt(N)) + 2): +            if f(b, N) == S: +                return b +        tmp = -1 +        for p in range(1, int(math.sqrt(N)) + 2): +            if (N - S) % p != 0: +                continue +            b = (N - S) // p + 1 +            if b <= 1: +                continue +            if f(b, N) == S: +                tmp = b +        return tmp + + +ans = abc(n, s) +print(ans)