Buggy python submission (verdict: Runtime Error):
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            cur = n
            ansC = 0
            while cur>0:
                ansC += cur%b
                cur = cur//b
            if ansC==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop,0,-1):
            bcandi = (n-s)//p + 1
            ansC = n%bcandi + n//bcandi
            if ansC==s:
                ans = int(bcandi)
                break
    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()


Fixed submission:
def examD():
    def f(b, n):
        if n < b:
            return n
        else:
            return f(b, n // b) + n % b
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            if f(b,n)==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop+1,0,-1):
            b = (n-s)//p + 1
            if b >= 2 and f(b,n)==s:
                    ans = b
                    break

    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Buggy python submission (verdict: Runtime Error):
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for i in range(2,loop+1):
            cur = n
            ansC = 0
            while cur>0:
                ansC += cur%i
                cur = cur//i
            if ansC==s:
                ans = i
                break
    if ans==-1:
        for p in range(loop,0,-1):
            bcandi = (n-s)/p + 1
            ansC = n%bcandi + n//bcandi
            if ansC==s:
                ans = int(bcandi)
                break
    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Fixed submission:
def examD():
    def f(b, n):
        if n < b:
            return n
        else:
            return f(b, n // b) + n % b
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            if f(b,n)==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop+1,0,-1):
            b = (n-s)//p + 1
            if b >= 2 and f(b,n)==s:
                    ans = b
                    break

    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Buggy python submission (verdict: Runtime Error):
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for i in range(2,loop+1):
            cur = n
            ansC = int(0)
            while cur>0:
                ansC += cur%i
                cur = cur//i
            if ansC==s:
                ans = i
                break
    if ans==-1:
        for p in range(1,loop+1):
            bcandi = (n-s)/p + 1
            ansC = n%bcandi + n//bcandi
            if ansC==s:
                ans = int(bcandi)
                break
    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Fixed submission:
def examD():
    def f(b, n):
        if n < b:
            return n
        else:
            return f(b, n // b) + n % b
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            if f(b,n)==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop+1,0,-1):
            b = (n-s)//p + 1
            if b >= 2 and f(b,n)==s:
                    ans = b
                    break

    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()