import sys

sys.setrecursionlimit(10**9)



def divisor(x):

    div=[]

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

        if x%i==0:

            div.append(i)

            if i!=x/i:

                div.append(x/i)

    return div



def f(b,n):

    if n<b:

        return n

    else:

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



n=int(input())

s=int(input())



if n==s:

    print((n+1))

    quit()



if n<s:

    print((-1))

    quit()



if n<=10**6:

    for i in range(2,n+2):

        if f(i,n)==s:

            print(i)

            quit()

    print((-1))

else:

    for i in range(2,10**6+1):

        if f(i,n)==s:

            print(i)

            quit()



    div=divisor(n-s)

    div.sort()

    for i in div:

        if i>=10**6:

            if f(i+1,n)==s:

                print((i+1))

                quit()

    print((-1))
import sys

sys.setrecursionlimit(10**9)



def divisor(x):

    div=[]

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

        if x%i==0:

            div.append(i)

            if i!=x/i:

                div.append(x/i)

    return div



def f(b,n):

    if n<b:

        return n

    else:

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



n=int(input())

s=int(input())



if n==s:

    print((n+1))

    quit()



if n<s:

    print((-1))

    quit()



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

    if f(i,n)==s:

        print(i)

        quit()



div=divisor(n-s)

div.sort()

for i in div:

    if f(i+1,n)==s:

        print((i+1))

        quit()

print((-1))
import sys

sys.setrecursionlimit(10**9)


def divisor(x):
    div = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            div.append(i)
            if i != x / i:
                div.append(x / i)
    return div


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


n = int(input())
s = int(input())
if n == s:
    print((n + 1))
    quit()
if n < s:
    print((-1))
    quit()
if n <= 10**6:
    for i in range(2, n + 2):
        if f(i, n) == s:
            print(i)
            quit()
    print((-1))
else:
    for i in range(2, 10**6 + 1):
        if f(i, n) == s:
            print(i)
            quit()
    div = divisor(n - s)
    div.sort()
    for i in div:
        if i >= 10**6:
            if f(i + 1, n) == s:
                print((i + 1))
                quit()
    print((-1))

import sys

sys.setrecursionlimit(10**9)


def divisor(x):
    div = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            div.append(i)
            if i != x / i:
                div.append(x / i)
    return div


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


n = int(input())
s = int(input())
if n == s:
    print((n + 1))
    quit()
if n < s:
    print((-1))
    quit()
for i in range(2, int(n**0.5) + 1):
    if f(i, n) == s:
        print(i)
        quit()
div = divisor(n - s)
div.sort()
for i in div:
    if f(i + 1, n) == s:
        print((i + 1))
        quit()
print((-1))

-if n <= 10**6: -    for i in range(2, n + 2): -        if f(i, n) == s: -            print(i) -            quit() -    print((-1)) -else: -    for i in range(2, 10**6 + 1): -        if f(i, n) == s: -            print(i) -            quit() -    div = divisor(n - s) -    div.sort() -    for i in div: -        if i >= 10**6: -            if f(i + 1, n) == s: -                print((i + 1)) -                quit() -    print((-1)) +for i in range(2, int(n**0.5) + 1): +    if f(i, n) == s: +        print(i) +        quit() +div = divisor(n - s) +div.sort() +for i in div: +    if f(i + 1, n) == s: +        print((i + 1)) +        quit() +print((-1))