Buggy python submission (verdict: Runtime Error):
n = int(input())
s = int(input())
import sys
sys.setrecursionlimit(20000)

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        x = f(b, n//b) + n%b
        return x
# print(f(100000000000,100000000000))
ans = 0
# if n<s:
#     print(-1)
#     exit()
if n==s:
    print(n+1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)//p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)


Fixed submission:
n = int(input())
s = int(input())

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        x = f(b, n//b) + n%b
        return x
ans = 0
if n<s:
    print(-1)
    exit()
if n==s:
    print(n+1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)//p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)

Buggy python submission (verdict: Runtime Error):
n = int(input())
s = int(input())

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        
        x = f(b, n//b) + n%b
        return x
ans = 0
if n<s:
    print(-1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)/p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)


Fixed submission:
n = int(input())
s = int(input())

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        x = f(b, n//b) + n%b
        return x
ans = 0
if n<s:
    print(-1)
    exit()
if n==s:
    print(n+1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)//p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)

Buggy python submission (verdict: Runtime Error):
n = int(input())
s = int(input())

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        
        x = f(b, n//b) + n%b
        return x
ans = 0
if n<s:
    print(-1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)/p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)


Fixed submission:
n = int(input())
s = int(input())

import math
def f(b, n):
    # print(b, n)
    if n<b:
        return n
    else:
        x = f(b, n//b) + n%b
        return x
ans = 0
if n<s:
    print(-1)
    exit()
if n==s:
    print(n+1)
    exit()
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        print(ans)
        exit()
for p in range(int(math.sqrt(n)), 0, -1):
    if (n+p-s) %p == 0:
        # print(p)
        if f((n+p-s)//p, n) == s:
            print((n+p-s)//p)
            exit()


print(-1)