Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
S=input()
#N=int(input())
#a,b,x=map(int,input().split())
s=1
for i in range(S):
    if i=="a":
        s*=2
    if i=="b":
        s*=3
    if i=="c":
        s*=5
if s==30:
    print("Yes")
else:
    print("No")

Fixed submission:
S=input()
#N=int(input())
#a,b,x=map(int,input().split())
s=1
for i in S:
    if i=="a":
        s*=2
    if i=="b":
        s*=3
    if i=="c":
        s*=5
if s==30:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
#S=input()
#N=int(input())
A,B,C,D=map(int,input().split())
print(min([A,B])+min([C,D]))

Fixed submission:
A=int(input())
B=int(input())
C=int(input())
D=int(input())
print(min([A,B])+min([C,D]))
Buggy python submission (verdict: Runtime Error):
import bisect
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=[]
for i in range(28):
  T=2**i
  B_=list(B)
  A_=list(A)
  t=0
  for j in range(N):
    B_[j]%=2*T
    A_[j]%=2*T
  B_.sort()
  for j in range(N):
    a=A_[j]
    p=bisect.bisect_left(B_,T-a)
    q=bisect.bisect_left(B_,2*T-a)
    r=bisect.bisect_left(B_,3*T-a)
    s=bisect.bisect_left(B_,4*T-a)
    t+=(q-p)+(s-r)
  ans.append(t%2)
a=0
for i in range(28):
  a+=2**i*ans[i]
print(a)


Fixed submission:
#S=input()
#N=int(input())
A,B,C=map(int,input().split())
if A+B>=C:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
c=[]
for i in range(3):
    ci=input()
    a.append(ci)
print(c[0][0]+c[1][1]+c[2][2])

Fixed submission:
c=[]
for i in range(3):
    ci=input()
    c.append(ci)
print(c[0][0]+c[1][1]+c[2][2])

Buggy python submission (verdict: Runtime Error):
c=[]
for i in range(3):
    ci=input()
    a.append(ci)
print(c[1][1]+c[2][2]+c[3][3])

Fixed submission:
c=[]
for i in range(3):
    ci=input()
    c.append(ci)
print(c[0][0]+c[1][1]+c[2][2])
Buggy python submission (verdict: Runtime Error):
from math import ceil
H,W,D=map(int,input().split())
A=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for i in range(H):
    Ai=list(map(int,input().split()))
    for j in range(W):

        A[Ai[j]%D][Ai[j]//D]=(i,j)
A[0][0]=(0,0)
B=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for j in range(D):
    for i in range(len(A[0])-1):
        if type(A[j][i+1])!=type((0,0)):
            B[j][i+1]=B[j][i]
            continue
        B[j][i+1]=B[j][i]+abs(A[j][i][0]-A[j][i+1][0])+abs(A[j][i][1]-A[j][i+1][1])
Q=int(input())
for _ in range(Q):
    L,R=map(int,input().split())
    print(B[R%D][R//D]-B[L%D][L//D])


Fixed submission:
from math import ceil
H,W,D=map(int,input().split())
A=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for i in range(H):
    Ai=list(map(int,input().split()))
    for j in range(W):
        A[(Ai[j]-1)%D][(Ai[j]-1)//D]=(i,j)
B=[[0]*(int(ceil(H*W/D))+1) for i in range(D)]
for j in range(D):
    for i in range(len(A[j])):
        if  type(A[j][i])!=type((0,0)):
            #B[j][i+1]=B[j][i]
            break
        if i==0:
            B[j][i+1]=A[j][i][0]+A[j][i][1]
            continue
        B[j][i+1]=B[j][i]+abs(A[j][i-1][0]-A[j][i][0])+abs(A[j][i-1][1]-A[j][i][1])
Q=int(input())
for _ in range(Q):
    L,R=map(int,input().split())
    L,R=L-1,R-1
    print(B[R%D][R//D+1]-B[L%D][L//D+1])

Buggy python submission (verdict: Runtime Error):
H,W,D=map(int,input().split())
A=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for i in range(H):
    Ai=list(map(int,input().split()))
    for j in range(W):

        A[Ai[j]%D][Ai[j]//D]=(i,j)
A[0][0]=(0,0)
B=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for j in range(D):
    for i in range(len(A[0])-1):
        if type(A[j][i+1])!=type((0,0)):
            B[j][i+1]=B[j][i]
            continue
        B[j][i+1]=B[j][i]+abs(A[j][i][0]-A[j][i+1][0])+abs(A[j][i][1]-A[j][i+1][1])
Q=int(input())
for _ in range(Q):
    L,R=map(int,input().split())
    print(B[R%D][R//D]-B[L%D][L//D])


Fixed submission:
from math import ceil
H,W,D=map(int,input().split())
A=[[0]*(int(ceil(H*W/D))) for i in range(D)]
for i in range(H):
    Ai=list(map(int,input().split()))
    for j in range(W):
        A[(Ai[j]-1)%D][(Ai[j]-1)//D]=(i,j)
B=[[0]*(int(ceil(H*W/D))+1) for i in range(D)]
for j in range(D):
    for i in range(len(A[j])):
        if  type(A[j][i])!=type((0,0)):
            #B[j][i+1]=B[j][i]
            break
        if i==0:
            B[j][i+1]=A[j][i][0]+A[j][i][1]
            continue
        B[j][i+1]=B[j][i]+abs(A[j][i-1][0]-A[j][i][0])+abs(A[j][i-1][1]-A[j][i][1])
Q=int(input())
for _ in range(Q):
    L,R=map(int,input().split())
    L,R=L-1,R-1
    print(B[R%D][R//D+1]-B[L%D][L//D+1])
Buggy python submission (verdict: Runtime Error):
N,C=map(int,input().split())
T=10**5+2
sm=[0]*(T)
l=[]
r=[]
c=[]
for i in range(N):
    s,t,k=map(int,input().split())
    l.append(s)
    r.append(t)
    c.append(k)
for i in range(1,C+1):
    tt=[0]*T
    for j in range(N):
        if c[j]==i:
            tt[l[j]*2]+=1
            tt[r[j]*2+1]-=1
    for j in range(T):
        if tt[j]>0:
            sm[j-1]+=tt[j]
        else:
            sm[j]+=tt[j]
for j in range(T-1):
    sm[j+1]+=sm[j]
print(max(sm))


Fixed submission:
from collections import defaultdict
from copy import deepcopy
from math import log
from itertools import accumulate

N,C=map(int,input().split())
T=10**5+1
programs=defaultdict(list)
for i in range(N):
    s,t,c=map(int,input().split())
    programs[c].append((s,t))
imos=[0]*(T)
for c,p in programs.items():
    p.sort()
    ps,pt=p[0]
    for s,t in p[1:]:
        if pt==s:
            pt=t
            continue
        imos[ps-1]+=1
        imos[pt]-=1
        ps,pt=s,t
    imos[ps-1]+=1
    imos[pt]-=1
print(max(accumulate(imos)))
Buggy python submission (verdict: Runtime Error):
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
  string c1,c2;
  cin >>c1>>c2;
  string c3=c1,c4=c2;
  reverse(c1.begin(),c1.end());
  reverse(c2.begin(),c2.end());
  if (c1==c4 and c2==c3) cout <<"YES"<<endl;
  else cout <<"NO"<<endl;
}


Fixed submission:

c=input()
d=input()
a=c
b=d
c=c[::-1]
d=d[::-1]
if a==d and b==c:
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from copy import deepcopy
from math import log
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
#n=int(ceil(log(K,2)))+1
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
ans=sum([b for a,b in AB if K|a==K])
print(bin(K))
for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    print(bin(m))
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)


Fixed submission:
S=input()
ans=0
for i in S:
    
    ans+=int(i)
print(ans)
Buggy python submission (verdict: Runtime Error):
N=int(input())
ans=0
for i in range(N):
    l,r=map(int,input().split())
    ans=ans+l-r+1
print(ans)


Fixed submission:
N=int(input())
if N//10==9 or N%10==9:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
B=[0]*(1+10**5)
for a in A:
    B[a+1]+=1
    B[a]+=1
    B[a+2]+=1
print(max(B))


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
B=[0]*(4+10**5)
for a in A:
    B[a+1]+=1
    B[a]+=1
    B[a+2]+=1
print(max(B))
Buggy python submission (verdict: Runtime Error):
N=int(input())
T=1
for i in range(N):
    T1=int(input())
    t=gcd(T,T1)
    T=(T1//t)*(T//t)*t
print(T)


Fixed submission:
from fractions import gcd
N=int(input())
T=1
for i in range(N):
    T1=int(input())
    t=gcd(T,T1)
    T=(T1//t)*(T//t)*t
print(T)
Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
G=[[0]*W for i in range(H)]
i=0
j=0
tmp=0
t=1
for a in A:
    tmp+=1
    for k in range(a):
        G[i][j]=tmp
        if j==W-1:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=-1
                t=0
        elif j==0:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=1
                t=0
        j+=dx
for k in G:
    print(*k,sep=" ")


Fixed submission:
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
if W==1:
    tmp=0
    for a in A:
        tmp+=1
        for j in range(a):
            print(tmp)
    quit()
G=[[0]*W for i in range(H)]
i=0
j=0
tmp=0
t=1
for a in A:
    tmp+=1
    for k in range(a):
        G[i][j]=tmp
        if j==W-1:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=-1
                t=0
        elif j==0:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=1
                t=0
        j+=dx
for k in G:
    print(*k,sep=" ")
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
dict=defaultdict(int)
l1=[]
l2=[]
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        l1.append(b)
    if b==N:
        dict[a]=1
for i in l1:
    if dict[i]:
        print("POSSIBLE")
        quit()
print("IMPOSSIBLE")


Fixed submission:
from collections import defaultdict
N,M=map(int,input().split())
dict=defaultdict(int)
l1=[]
l2=[]
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        l1.append(b)
    if b==N:
        dict[a]=1
for i in l1:
    if dict[i]:
        print("POSSIBLE")
        quit()
print("IMPOSSIBLE")
Buggy python submission (verdict: Runtime Error):
A,B=map(int,input().split())
if A%3==0 or B%3==0 or (A+B)%3==0:
    print(Possible)
else:
    print(Impossible)

Fixed submission:
A,B=map(int,input().split())
if A%3==0 or B%3==0 or (A+B)%3==0:
    print("Possible")
else:
    print("Impossible")
Buggy python submission (verdict: Runtime Error):
N=int(input())
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
stack1=deque(G[0])
stack2=deque(G[N-1])
X=[-1]*N
X[0]=1
X[N-1]=0
while(stack1 or stack2):
    Stack1=deque()
    for i in stack1:
        if X[i]==-1:
            X[i]=1
            for j in G[i]:
                if X[j]==-1:
                    Stack1.append(j)
    stack1=Stack1
    Stack2=deque()
    for i in stack2:
        if X[i]==-1:
            X[i]=0
            for j in G[i]:
                if X[j]==-1:
                    Stack2.append(j)
    stack2=Stack2
f,s=0,0
for i in range(N):
    if X[i]==1:
        f+=1
    else:
        s+=1
if f>s:
    print("Fennec")
else:
    print("Sunuke")


Fixed submission:
from collections import deque
N=int(input())
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
stack1=deque(G[0])
stack2=deque(G[N-1])
X=[-1]*N
X[0]=1
X[N-1]=0
while(stack1 or stack2):
    Stack1=deque()
    for i in stack1:
        if X[i]==-1:
            X[i]=1
            for j in G[i]:
                if X[j]==-1:
                    Stack1.append(j)
    stack1=Stack1
    Stack2=deque()
    for i in stack2:
        if X[i]==-1:
            X[i]=0
            for j in G[i]:
                if X[j]==-1:
                    Stack2.append(j)
    stack2=Stack2
f,s=0,0
for i in range(N):
    if X[i]==1:
        f+=1
    else:
        s+=1
if f>s:
    print("Fennec")
else:
    print("Snuke")
Buggy python submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

N=int(input())
X=[]
Y=[]
for i in range(N):
    x,y=map(int,input().split())
    X.append((x,i))
    Y.append((y,i))
X.sort()
Y.sort()
E=[]
ans=0
count=1
for i in range(N-1):
    a=X[i]
    b=X[i+1]
    c=Y[i]
    d=Y[i+1]
    E.extend([(b[0]-a[0],X[i][1],X[i+1][1]),(d[0]-c[0],Y[i][1],Y[i+1][1])])
E.sort()

U=UnionFind(N)
i=0
while(count<N):
    cost,v1,v2=E[i]
    if not U.same_check(v1,v2):
        U.union(v1,v2)
        ans+=cost
        count+=1
    i+=1
print(ans)


Fixed submission:
X,A,B=map(int,input().split())
if B-A>X:
    print("dangerous")
elif B-A>0:
    print("safe")
else:
    print("delicious")
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
c=[0]*9
for i in A:
    c[i//400]+=1
for i in range(8):
    if c[i]>0:
        c[i]=1
if c[:8]==[0]*8:
    print(min(1,c[-1]),c[-1])
else:
    print(sum(c[:8]),sum(c[:8])+c[-1])


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
c=[0]*9
for i in A:
    c[min(i//400,8)]+=1
for i in range(8):
    if c[i]>0:
        c[i]=1
if c[:8]==[0]*8:
    print(1,c[-1])
else:
    print(sum(c[:8]),sum(c[:8])+c[-1])
Buggy python submission (verdict: Runtime Error):
from heapq import *

N=int(input())
A=map(int,input().split())
A.reverse()
B=[]
heap=[]
for i in range(N):
    heappush(heap,A[i])
for i in range(N,2*N):
    if heap[0]<A[i]:
        B.append(heappop(heap))
        heappush(heap,A[i])
    else:
        B.append(A[i])
C=[]
for i in range(2*N,3*N):
    if 2*B[-1]-A[i]-heap[0]>0:
        C.append(B.pop(-1))
        B.append(heappop(heap))
    else:
        C.append(A[i])
print(sum(C)-sum(B))


Fixed submission:
from heapq import *

N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
    heappush(B,A[i])
sumB=[sum(B)]
for i in range(N,2*N):
    heappush(B,A[i])
    poped=heappop(B)
    sumB.append(sumB[-1]-poped+A[i])

A.reverse()
C=[]
for i in range(N):
    heappush(C,A[i]*(-1))
sumC=[(-1)*sum(C)]
for i in range(N,2*N):
    heappush(C,A[i]*(-1))
    poped=heappop(C)*(-1)
    sumC.append(sumC[-1]-poped+A[i])
sumC.reverse()
ans=-10**19
for i,j in zip(sumB,sumC):
    ans=max(ans,i-j)
print(ans)
Buggy python submission (verdict: Runtime Error):
n=int(input())
l=[defaultdict(int) for i in range(n)]
for i in range(n):
    S=input()
    for s in S:
        l[i][s]+=1
t=""
for i in range(26):
    a=10**30
    for j in range(n):
        a=min(a,l[j][chr(i+ord("a"))])
    t+=chr(i+ord("a"))*a
print(t)


Fixed submission:
from collections import defaultdict

n=int(input())
l=[defaultdict(int) for i in range(n)]
for i in range(n):
    S=input()
    for s in S:
        l[i][s]+=1
t=""
for i in range(26):
    a=10**30
    for j in range(n):
        a=min(a,l[j][chr(i+ord("a"))])
    t+=chr(i+ord("a"))*a
print(t)
Buggy python submission (verdict: Runtime Error):
import itertools

N,M=map(int,input().split())
ab=[]
for i in range(N):
    a,b=map(int,input().split())
    ab.append([a,b])

per=[i for i in range(2,N+1)]
permu=[]
for i in itertools.permutations(per):
    lst=[1]
    lst.extend(i)
    permu.append(lst)
num=0
for lst in permu:
    can=True
    for i in range(N-1):
        if not([lst[i],lst[i+1]] in ab) and not([lst[i+1],lst[i]] in ab):
            can=False
            break
    if can:
        num+=1
print(num)


Fixed submission:
import itertools

N,M=map(int,input().split())
ab=[]
for i in range(M):
    a,b=map(int,input().split())
    ab.append([a,b])

per=[i for i in range(2,N+1)]
permu=[]
for i in itertools.permutations(per):
    lst=[1]
    lst.extend(i)
    permu.append(lst)
num=0
for lst in permu:
    can=True
    for i in range(N-1):
        if not([lst[i],lst[i+1]] in ab) and not([lst[i+1],lst[i]] in ab):
            can=False
            break
    if can:
        num+=1
print(num)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N,M=map(int,input().split())
R=[]
G=[[] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    G[a].append((b,c))
    G[b].append((a,c))
    R.append((a,b,c))
Q=[[0]*N for i in range(N)]
INF=10**9
for i in range(N):
    stack=[]
    g=[INF for i in range(N)]
    g[i]=0
    for b,c in G[i]:
        heapq.heappush(stack,(g[i]+c,i,b))
    used=[[0]*N for i in range(N)]
    while(stack):
        c,a,b=heapq.heappop(stack)
        g[b]=min(g[b],c)
        used[a][b]=1
        used[b][a]=1
        for j in G[b]:
            if used[b][j[0]]:
                continue
            heapq.heappush(stack,(g[b]+j[1],b,j[0]))
    for j in range(i+1,N):
        Q[i][j]=g[j]
ans=0
for a,b,c in R:
    if b<a:
        a,b=b,a
    if Q[a][b]<c:
        ans+=1
print(ans)


Fixed submission:
import heapq
N,M=map(int,input().split())
R=[]
G=[[] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    G[a].append((b,c))
    G[b].append((a,c))
    R.append((a,b,c))
Q=[[0]*N for i in range(N)]
INF=10**9
for i in range(N):
    stack=[]
    g=[INF for i in range(N)]
    g[i]=0
    n=1
    for b,c in G[i]:
        heapq.heappush(stack,(g[i]+c,i,b))
    used=[[0]*N for i in range(N)]
    while(stack):
        if n==N:
            break
        c,a,b=heapq.heappop(stack)
        g[b]=min(g[b],c)
        used[a][b]=1
        used[b][a]=1
        for j in G[b]:
            if used[b][j[0]] or g[j[0]]!=INF:
                continue
            heapq.heappush(stack,(g[b]+j[1],b,j[0]))
    for j in range(i+1,N):
        Q[i][j]=g[j]
ans=0
for a,b,c in R:
    if b<a:
        a,b=b,a
    if Q[a][b]<c:
        ans+=1
print(ans)
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
A.sort()
INF=1e9+7
if N%2==1:
    if A[0]!=0:
        print(0)
        quit()
    for i in range((N-1)//2):
        if A[2*i+1]!=2*(i+1) or A[2*i+2]!=2*(i+1):
            print(0)
            quit()
    print(2**((N-1)//2)%INF)
else:
    for i in range(N//2):
        if A[2*i]!=2*i+1 or A[2*i+1]!=2*i+1:
            print(0)
            quit()
    print(2**(N//2)%INF)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
A.sort()
INF=int(1e9+7)
if N%2==1:
    if A[0]!=0:
        print(0)
        quit()
    for i in range((N-1)//2):
        if A[2*i+1]!=2*(i+1) or A[2*i+2]!=2*(i+1):
            print(0)
            quit()
    print(2**((N-1)//2)%INF)
else:
    for i in range(N//2):
        if A[2*i]!=2*i+1 or A[2*i+1]!=2*i+1:
            print(0)
            quit()
    print(2**(N//2)%INF)
Buggy python submission (verdict: Runtime Error):
S=input()
ans=0
for i in range(2**(len(S)-1)):
    k=bin(i)[2:].zfill(len(S)-1)
    l=int(S[0])
    for j in range(len(k)):
        t=int(k[j])
        if t==1:
            ans+=l
            l=int(S[j+1])
        else:
            l=l*10+int(S[j+1])
    ans+=l
print(ans)


Fixed submission:
import copy
S=input()
lst=[]
def Sum(l,S):
    if S=="A":
        return sum(l)
    a=l.pop()
    x=copy.copy(l)
    x.append(a*10+int(S[0]))
    y=copy.copy(l)
    y.extend([a,int(S[0])])
    return Sum(x,S[1:])+Sum(y,S[1:])
S+="A"
lst.append(int(S[0]))
print(Sum(lst,S[1:]))
Buggy python submission (verdict: Time Limit Exceeded):
from math import ceil
from itertools import permutations

#S=input()
#D=list(map(int,input().split()))
from heapq import *
"""
import sys
input=sys.stdin.readline
"""
from collections import defaultdict
from fractions import gcd
#S=
from math import sqrt
import sys
sys.setrecursionlimit(100100)

from collections import deque
import bisect
import sys
input=sys.stdin.readline

n=int(input())
s=int(input())
if s>n:
    print(-1)
    quit()
if s==n :
    print(n+1)
    quit()
def func(b,n):
    res=0
    while(n):
        res+=n%b
        n//=b
    return res
j=1
i=n
ans=10**30
for i in range(2,int(sqrt(n))+1):
    if func(i,n)==s:
        ans=min(ans,i)
for i in range(1,ceil(sqrt(n))):
    b=(n-s)//i+1
    if func(b,n)==s:
        ans=min(ans,b)
if ans==10**30:
    print(-1)
else:
    print(ans)


Fixed submission:
from math import ceil
from itertools import permutations

#S=input()
#D=list(map(int,input().split()))
from heapq import *
"""
import sys
input=sys.stdin.readline
"""
from collections import defaultdict
from fractions import gcd
#S=
from math import sqrt
import sys
sys.setrecursionlimit(100100)

from collections import deque
import bisect
import sys
input=sys.stdin.readline

n=int(input())
s=int(input())
if s>n:
    print(-1)
    quit()
if s==n :
    print(n+1)
    quit()
def func(b,n):
    res=0
    while(n):
        res+=n%b
        n//=b
    return res
j=1
i=n
ans=10**30
for i in range(2,int(sqrt(n))+1):
    if func(i,n)==s:
        ans=min(ans,i)
l=defaultdict()
for i in range(1,ceil(sqrt(n))):
    b=(n-s)//i+1
    if sqrt(n)<b and n//b+(n%b)==s:
        ans=min(ans,b)
if ans==10**30:
    print(-1)
else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input(),split())
print(max(a+b,a-b,a*b))

Fixed submission:
a,b=map(int,input().split())
print(max(a+b,a-b,a*b))

Buggy python submission (verdict: Runtime Error):
a, b = int(input().split())
m = a + b
m = m < a - b if m = a - b else m
m = m < a * b if m = a * b else m
print(m)

Fixed submission:
a,b=map(int,input().split())
print(max(a+b,a-b,a*b))
Buggy python submission (verdict: Time Limit Exceeded):
a, b = (int(x) for x in input().split())
m = max(a, b)
for k in range(m):
    if abs(a-k) == abs(b-k):
        break
if abs(a-k) == abs(b-k):
    print(k)
else:
    print("IMPOSSIBLE")

Fixed submission:
a, b = (int(x) for x in input().split())
ans = int(a/2+b/2+0.5)
if abs(a-ans) == abs(b-ans):
    print(ans)
else:
    print("IMPOSSIBLE")

Buggy python submission (verdict: Runtime Error):
ba, bb = (int(x) for x in input().split())
m = a if a > b else b
for i in range(m+2):
    a = abs(a-i)
    b = abs(b-i)
    if a == b:
        print(i)
        break:
if i == m+1:
    print('IMPOSSIBLE')

Fixed submission:
a, b = (int(x) for x in input().split())
ans = int(a/2+b/2+0.5)
if abs(a-ans) == abs(b-ans):
    print(ans)
else:
    print("IMPOSSIBLE")

Buggy python submission (verdict: Runtime Error):
ba, bb = (int(x) for x in int(input().split()))
m = a if a > b else b
for i in range(m+2):
    a = abs(a-i)
    b = abs(b-i)
    if a == b:
        print(i)
if i == m+1:
    print('IMPOSSIBLE')

Fixed submission:
a, b = (int(x) for x in input().split())
ans = int(a/2+b/2+0.5)
if abs(a-ans) == abs(b-ans):
    print(ans)
else:
    print("IMPOSSIBLE")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [0]*(n)
for i in range(n):
    a[i] = int(input())
for i in range(n):
    max = 0
    for j in range(n):
        if j == i:
            continue
        if a[j] > max:
            max = a[j]
    print(max)

Fixed submission:
n = int(input())
a = [int(input()) for i in range(n)]
num = a.index(max(a))
a_max = max(a)
a_second = sorted(a)[-2]
for i in range(n):
    if i == num:
        print(a_second)
    else:
        print(a_max)
Buggy python submission (verdict: Runtime Error):
date = inp.readline().rstrip().split(' ')
x = int(date[0])
y = int(date[1])
num1 = x + y
num2 = x - y
num3 = x * y

if num1 > num2:
    if num1 > num3:
        ans = num1
    else:
        ans = num3
elif num2 > num3:
    ans = num2
else:
    ans = num3

print(ans)

Fixed submission:
x, y = list(map(int, input().split()))

num1 = x + y
num2 = x - y
num3 = x * y

if num1 > num2:
    if num1 > num3:
        ans = num1
    else:
        ans = num3
elif num2 > num3:
    ans = num2
else:
    ans = num3

print(ans)