import math

import sys



n = int(eval(input()))

s = int(eval(input()))



def f(b, n):

    if n == 0:

        return 0

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



if s > n:

    print((-1))

    sys.exit(0)



sn = math.ceil(math.sqrt(n))

for i in range(2, sn+1):

    if f(i, n) == s:

        print(i)

        sys.exit(0)



ans = n + 1

for i in range(1, sn+1):

    if (n - s) % i == 0:

        b = (n-s)//i + 1

        if b >= 2 and f(b, b * i + n%b) == s:

            ans = min((ans, b))

if s == n:

    print((n+1))

elif ans < n + 1:

    print(ans)

else:

    print((-1))

import sys

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



if s == n:

    print((n+1))

    sys.exit(0)

if n < s:

    print((-1))

    sys.exit(0)

     

ans = None

for i in range(2, n):

    if i * i > n:

        break

    if f(i, n) == s:

        ans = i

        break



for i in range(1, n):

    if i * i > n:

        break

    if (n - s) % i == 0:

        b = (n - s) // i + 1

        if f(b, n) == s:

            if ans is None:

                ans = (n - s) // i + 1

            else:

                ans = min((ans, (n - s) // i + 1))



if ans is None:

    print((-1))

else:

    print(ans)

import math
import sys

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


def f(b, n):
    if n == 0:
        return 0
    return f(b, n // b) + n % b


if s > n:
    print((-1))
    sys.exit(0)
sn = math.ceil(math.sqrt(n))
for i in range(2, sn + 1):
    if f(i, n) == s:
        print(i)
        sys.exit(0)
ans = n + 1
for i in range(1, sn + 1):
    if (n - s) % i == 0:
        b = (n - s) // i + 1
        if b >= 2 and f(b, b * i + n % b) == s:
            ans = min((ans, b))
if s == n:
    print((n + 1))
elif ans < n + 1:
    print(ans)
else:
    print((-1))

import sys

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


if s == n:
    print((n + 1))
    sys.exit(0)
if n < s:
    print((-1))
    sys.exit(0)
ans = None
for i in range(2, n):
    if i * i > n:
        break
    if f(i, n) == s:
        ans = i
        break
for i in range(1, n):
    if i * i > n:
        break
    if (n - s) % i == 0:
        b = (n - s) // i + 1
        if f(b, n) == s:
            if ans is None:
                ans = (n - s) // i + 1
            else:
                ans = min((ans, (n - s) // i + 1))
if ans is None:
    print((-1))
else:
    print(ans)

-import math -    if n == 0: -        return 0 -    return f(b, n // b) + n % b +    if n < b: +        return n +    else: +        return f(b, n // b) + n % b -if s > n: +if s == n: +    print((n + 1)) +    sys.exit(0) +if n < s: -sn = math.ceil(math.sqrt(n)) -for i in range(2, sn + 1): +ans = None +for i in range(2, n): +    if i * i > n: +        break -        print(i) -        sys.exit(0) -ans = n + 1 -for i in range(1, sn + 1): +        ans = i +        break +for i in range(1, n): +    if i * i > n: +        break -        if b >= 2 and f(b, b * i + n % b) == s: -            ans = min((ans, b)) -if s == n: -    print((n + 1)) -elif ans < n + 1: +        if f(b, n) == s: +            if ans is None: +                ans = (n - s) // i + 1 +            else: +                ans = min((ans, (n - s) // i + 1)) +if ans is None: +    print((-1)) +else: -else: -    print((-1))