Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03419 (python)

Fixed submission:
N,M = map(int, input().split())
if N == 1: print(1) if M == 1 else print(M - 2)
elif M == 1: print(N - 2)
else: print(N * M - (N + M - 2) * 2)

Buggy submission (verdict: Runtime Error):
N,M = map(int, input().split())
if n == 1:
    if m == 1:
      print(1)
    else:
      print(m - 2)
elif m == 1:
  print(n - 2)
else: 
  print((N-2)*(M-2))
Problem p03434 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
A.sort()
B = []
for i in range(0,N,2):
  B.append(A[i])
print(abs((sum(A)-sum(B))-sum(B)))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
B = []
for i in range(0,N,2):
  B.append(A[i])
print(abs(A-sum(B)*2))
Problem p03469 (python)

Fixed submission:
a = input().split("/")
print("2018/"+a[1]+"/"+a[2])

Buggy submission (verdict: Runtime Error):
a = input().split()
print("2018/"+a[1]+"/"+a[2])
Problem p03486 (python)

Fixed submission:
import sys
S = "".join(sorted(input()))
T = "".join(sorted("".join(sorted(input())), reverse=True))
print("Yes" if S < T else "No")

Buggy submission (verdict: Runtime Error):
import sys
S = [ord(i) for i in input()]
T = [ord(j) for j in input()]
S.sort()
T = sorted(T, reverse=True)
#print(S,T)
for k in range(len(S)):
  if S[k] > T[k]:
    print("No")
    sys.exit()
  elif S[k] < T[k]:
    print("Yes")
    sys.exit()
  else:
    None
if len(S) <= len(T):
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
import sys
S = [ord(i) for i in input()]
T = [ord(j) for j in input()]
S.sort()
T.sort()
for k in range(len(S)):
  if S[k] > T[k]:
    print("No")
    sys.exit()
  elif S[k] < T[k]:
    print("Yes")
    sys.exit()
  else:
    None
if len(S) <= len(T):
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
impport sys
S = [ord(i) for i in input()]
T = [ord(j) for j in input()]
S.sort()
T.sort()
for k in range(len(S)):
  if S[k] > T[k]:
    print("No")
    sys.exit()
  elif S[k] < T[k]:
    print("Yes")
    sys.exit()
  else:
    None
if len(S) <= len(T):
  print("Yes")
else:
  print("No")
Problem p03502 (python)

Fixed submission:
N = input()
X = 0
for i in range(len(N)):
  X += int(N[i])
if int(N) % X == 0:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
N = input()
X = 0
for i in range(len(N)):
  X += int(N[i])
if N % X == 0:
  print("Yes")
else:
  print("No")
Problem p03523 (python)

Fixed submission:
S=input()

pos={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}
     
if S in pos:
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
S = input()
if len(S) >= 10:
  print("NO")
else:
  if S == "AKIHABARA":
    print("YES")
  elif S == "KIHABARA":
    print("YES")
  elif S == "AKIHBARA":
    print("YES")
  elif S == "AKIHABRA":
    print("YES")
  elif S == "AKIHABR":
    print("YES")
  elif S == "KIHBARA":
    print("YES")
  elif S == "KIHABRA":
    print("YES")
  elif S == "KIHABAR":
    print("YES")
  elif S == "AKIHBRA":
    print("YES")
  elif S == "AKIHABR":
    print("YES")
  elif S == "KIHBRA":
    print("YES")
  elif S == "KIHBAR":
    print("YES")
  elif S == "AKIHBR":
    print("YES")
  elif S == "KIHBR":
    print("YES")
  else:
  print("NO")

Buggy submission (verdict: Runtime Error):
S = input()
if len(S) >= 10:
  print("NO")
else:
  if S == "AKIHABARA":
    print("YES")
  elif S == "KIHABARA":
    print("YES")
  elif S == "AKIHBARA":
    print("YES")
  elif S == "AKIHABRA":
    print("YES")
  elif S == "AKIHABR":
    print("YES")
  elif S == "KIHBARA":
    print("YES")
  elif S == "KIHABRA":
    print("YES")
  elif S == "KIHABAR":
    print("YES")
  elif S == "AKIHBRA":
    print("YES")
  elif S == "AKIHABR":
    print("YES")
  elif S == "KIHBRA":
    print("YES")
  elif S == "KIHBAR":
    print("YES")
  elif S == "AKIHBR":
    print("YES")
  elif S == "KIHBR":
    print("YES")
  else;
  print("NO")
Problem p03556 (python)

Fixed submission:
N = int(input())
result = 0
for i in range(int(N**0.5)+1):
  if i**2 <= N:
    result = i
  else:
    None
print(result**2)

Buggy submission (verdict: Runtime Error):
N = int(input())
result = 0
for i in range(N**0.5):
  if i**2 <= N:
    result = i
  else:
    None
print(result)
Problem p03588 (python)

Fixed submission:
N = int(input())
M = 0
m = 10**9
c = 0
for i in range(N):
  a,b = map(int,input().split())
  if m >= a:
    m = a
  if M < a:
    M = a
    c = b + 1
ans = c+M-1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
M = 0
m = 10**9
c = 0
for i in range(N):
  a,b = map(int.input().split())
  if m >= a:
    m = a
  if M < a:
    M = a
    c = b + 1
print(c+m-M)
Problem p03637 (python)

Fixed submission:
N = int(input())
d4 = 0
d2 = 0
for i in list(map(int,input().split())):
    if i % 4 == 0:
        d4 += 1
    elif i % 2 == 0:
        d2 += 1
    else:
        None
if N <= d4*2 + 1:
    print("Yes")
elif N - d4 * 2 <= d2 :
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
for i in range(N):
    A[i] = A[i] % 4
if A.count(2) % 2 == 0:
    if A.count(4) >= (A.count(1)+A.count(3)):
        print("Yes")
    else:
        print("No")
else:
    if A.count(4) >= (A.count(1)+A.count(3)) + 1:
    print("Yes")                         
else:                                    
    print("No")
Problem p03680 (python)

Fixed submission:
import sys
N = int(input())
A ={}
for i in range(1,N+1):
    A[i] = int(input())
#print(A)
ans = 1
x = 1
for j in range(N):
    if A[x] == 2:
        print(ans)
        sys.exit()
    x = A[x]
    ans += 1
print(-1)

Buggy submission (verdict: Runtime Error):
import sys
N = int(input())
A = [int(input()) for _ in range(N)]
ans = 1
a = A.index(1)+1
#print(a)
for i in range(N*2):
    a = A[a-1]
    #print(a)
    if a == 2:
        print(ans)
        sys.exit()
    ans += 1
print(-1)
Problem p03729 (python)

Fixed submission:
word1,word2,word3 = map(str,input().split())
if word1[-1] == word2[0] and word2[-1] == word3[0]:
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
word1,word2,word3 = map(str,input().split())
if word1[-1] == word2[1] and word2[-1] == word3[1]:
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
word1,word2,word3 = map(str,input().split())
if word1[-1] == word2[1] and word1[-1] == word2[1]:
  print("YES")
else:
  print("NO")
Problem p03838 (python)

Fixed submission:
x,y = map(int,input().split())
if abs(x) == abs(y):
  print(0 if x == y else 1)
elif x < y:
  print(min(y - x,abs(y + x) + 1))
else:
  print(min(abs(y+x),abs(y-x))+ 1 if x * y <= 0 else min(abs(y+x),abs(y-x))+ 2)

Buggy submission (verdict: Runtime Error):
x,y = map(int,input().split())
if abs(x) == abs(y):
  print(0 if a == b else 1)
elif x < y:
  print(min(y - x,abs(y + x) + 1))
else:
  print(min(abs(y+x),abs(y-x))+ 1 if a * b <= 0 else min(abs(y+x),abs(y-x))+ 2)

Buggy submission (verdict: Runtime Error):
x,y = map(int,input().split())
ans = abs(abs(x)-abs(y))
print(ans if X * y > 0 else ans + 1)

Buggy submission (verdict: Runtime Error):
x,y = map(int,input().split())
print(min(abs(y-x),abs(x+y)+1)
Problem p03943 (python)

Fixed submission:
A = list(map(int,input().split()))
A.sort()
if A[0]+A[1] == A[2]:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
A = map(int,input().split())
A.sort()
if A[0]+A[1] == A[2]:
  print("Yes")
else:
  print("No")
Problem p04012 (python)

Fixed submission:
import sys
S = input()
A = set([i for i in S])
for j in A:
  if S.count(j) % 2 == 1:
    print("No")
    sys.exit()
  else:
    None
print("Yes")

Buggy submission (verdict: Runtime Error):
S = input()
A = set([i for i in S])
for j in range(len(A)):
  if S.count(A[j]) % 2 == 1:
    print("Yes")
    sys.exit()
  else:
    None
print("No")
Problem p04019 (python)

Fixed submission:
s = input()
print("No" if (s.count("N") == 0 and s.count("S") > 0)\
		   or (s.count("S") == 0 and s.count("N") > 0)\
	       or (s.count("W") == 0 and s.count("E") > 0)\
		   or (s.count("E") == 0 and s.count("W") > 0)\
		   else "Yes")

Buggy submission (verdict: Runtime Error):
S = input()
print("No" if (s.count("N") == 0 and s.count("S") > 0)\
		or (s.count("S") == 0 and s.count("N") > 0)\
		or (s.count("W") == 0 and s.count("E") > 0)\
		or (s.count("E") == 0 and s.count("W") > 0)\
		else "Yes")

Buggy submission (verdict: Runtime Error):
S = input()
print("No" if(s.count("N") == 0 and s.count("S") > 0)\
		or (s.count("S") == 0 and s.count("N") > 0)\
		or (s.count("W") == 0 and s.count("E") > 0)\
		or (s.count("E") == 0 and s.count("W") > 0)\
		else "Yes")
Problem p02555 (python)

Fixed submission:
N = int(input())

MOD_CONST = 10 ** 9 + 7

if N <= 2:
    print(0)
    exit()

res = [1] * N
res[0] = res[1] = 0

for i in range(5, N):
    res[i] = res[i - 4] + res[i - 3] + res[i - 2]

print(res[N-1] % MOD_CONST)

Buggy submission (verdict: Runtime Error):
N = int(input())

MOD_CONST = 10 ** 9 + 7

res = [1] * N
res[0] = res[1] = 0

for i in range(5, N):
    res[i] = res[i - 4] + res[i - 3] + res[i - 2]

print(res[N-1] % MOD_CONST)
Problem p02609 (python)

Fixed submission:
N = int(input())
X = input()

cnt = 0
for i in range(N):
    if X[i] == "1":
        cnt += 1
    
down = [0] * N
up = [0] * N
ald = 0
alu = 0
for i in range(N):
    up[i] = pow(2, N-1-i, cnt+1)
    if X[i] == "1":
        alu += up[i]
        alu %= cnt+1
    if cnt <= 1:
        continue
    down[i] = pow(2, N-1-i, cnt-1)
    if X[i] == "1":
        ald += down[i]
        ald %= cnt-1

for i in range(N):    
    if X[i] == "1":
        if cnt == 1:
            print(0)
            continue
        n = (ald - down[i]) % (cnt-1)
    else:
        n = (alu + up[i]) % (cnt+1)
    ans = 1
    while n != 0:
        c = 0
        for j in range(20):
            if (n >> j) % 2 == 1:
                c += 1
        n %= c
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = input()

cnt = 0
for i in range(N):
    if X[i] == "1":
        cnt += 1
    
down = [0] * N
up = [0] * N
ald = 0
alu = 0
for i in range(N):
    down[i] = pow(2, N-1-i, cnt-1)
    up[i] = pow(2, N-1-i, cnt+1)
    if X[i] == "1":
        ald += down[i]
        ald %= cnt-1
        alu += up[i]
        alu %= cnt+1

for i in range(N):
    if X[i] == "1":
        n = (ald - down[i]) % (cnt-1)
    else:
        n = (alu + up[i]) % (cnt+1)
    ans = 1
    while n != 0:
        c = 0
        for j in range(20):
            if (n >> j) % 2 == 1:
                c += 1
        n %= c
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = input()

cnt = 0
for i in range(N):
    if X[i] == "1":
        cnt += 1
    
down = [0] * N
up = [0] * N
ald = 0
alu = 0
for i in range(N):
    down[i] = pow(2, N-1-i, cnt-1)
    up[i] = pow(2, N-1-i, cnt+1)
    if X[i] == "1":
        ald += down[i]
        ald %= cnt-1
        alu += up[i]
        alu %= cnt+1

for i in range(N):
    if X[i] == "1":
        n = (ald - down[i]) % (cnt-1)
    else:
        n = (alu + up[i]) % (cnt+1)
    ans = 1
    while n != 0:
        c = 0
        for j in range(18):
            if (n >> j) % 2 == 1:
                c += 1
        n %= c
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
X = input()

x = Counter(X)
cnt = x["1"]

down = [0] * N
up = [0] * N
ald = 0
alu = 0
for i in range(N):
    down[i] = pow(2, N-1-i, cnt-1)
    up[i] = pow(2, N-1-i, cnt+1)
    if X[i] == "1":
        ald += down[i]
        ald %= cnt-1
        alu += up[i]
        alu %= cnt+1

for i in range(N):
    if X[i] == "1":
        n = (ald - down[i]) % (cnt-1)
    else:
        n = (alu + up[i]) % (cnt+1)
    ans = 1
    while n != 0:
        c = 0
        for j in range(18):
            if (n >> j) % 2 == 1:
                c += 1
        n %= c
        ans += 1
    print(ans)
Problem p02691 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

cnt = [0] * (2*(10 ** 5) + 10) 
for i in range(N):
    if A[i]+i >= 2*(10 ** 5):
        continue
    cnt[A[i]+i] += 1

ans = 0
for i in range(N)[1:]:
    if A[i]-i >= 0:
        continue
    ans += cnt[abs(A[i]-i)]

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

cnt = [0] * (2*(10 ** 5) + 10) 
for i in range(N):
    if A[i]+1 >= 2*(10 ** 5):
        continue
    cnt[A[i]+i] += 1

ans = 0
for i in range(N)[1:]:
    if A[i]-i >= 0:
        continue
    ans += cnt[abs(A[i]-i)]

print(ans)
Problem p02727 (python)

Fixed submission:
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort()

p = p[:X]
a = q[:Y] + p
z = []
a.sort(reverse = True)

while len(a) != 0 and len(r) != 0 and r[-1] > a[-1]:
    k = r.pop()
    del a[-1]
    z.append(k)   

print(sum(a) + sum(z))

Buggy submission (verdict: Runtime Error):
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort()

p = p[:X]
q = q[:Y]
z = []

while len(r) != 0 and r[-1] > min(p[-1], q[-1]):
    k = r.pop()
    if p[-1] < q[-1]:
        del p[-1]
    else:
        del q[-1]
    z.append(k)   

print(sum(p) + sum(q) + sum(z))
Problem p02855 (python)

Fixed submission:
from itertools import accumulate

H, W, K = map(int, input().split())
s = [input() for _ in range(H)]

ans = []
acc = 0
l = 0
for i in s:
    cnt = i.count("#")
    if cnt == 0:
        if not ans:
            l += 1
        else:
            ans.append(ans[-1])
    else:
        k = [0] * W
        for j in range(W):
            if i[j] == "#":
                k[j] = 1
        k = list(accumulate(k))
        for j in range(W):
            if k[j] == 0:
                k[j] = 1
            k[j] += acc
        ans.append(k)
    if ans and l >= 1 :
        for _ in range(l):
            ans.append(ans[-1])
        l = 0
    acc += cnt
        
for i in ans:
    print(" ".join(map(str, i)))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate

H, W, K = map(int, input().split())
s = [input() for _ in range(H)]

ans = []
acc = 0
l = 0
for i in s:
    cnt = i.count("#")
    if cnt == 0:
        if not ans:
            l += 1
        else:
            ans.append(ans[-1])
    else:
        k = [0] * W
        for j in range(W):
            if i[j] == "#":
                k[j] = 1
        k = list(accumulate(k))
        for j in range(W):
            if k[j] == 0:
                k[j] = 1
            k[j] += acc
        ans.append(k)
    if l >= 1:
        for _ in range(l):
            ans.append(ans[-1])
        l = 0
    acc += cnt
        
for i in ans:
    print(" ".join(map(str, i)))
Problem p02890 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from collections import Counter
from itertools import accumulate

N = int(input())
A = list(map(int,input().split()))

B = list(Counter(A).values())

C = [0] * N 
for i in B:
    C[i-1] += 1

F = list(accumulate(C))

S = [0] * N
f = [0] * N
S[0] = f[0] = sum(C) 
for i in range(2, N+1):
    S[i-1] = (S[i-2] + C[i-1] + S[0] - F[i-1] ) 
    f[i-1] = S[i-1] / i

f.sort()
n = 0     
K = 1
    
while K != N+1:
    if K <= f[n]:
        print(N-n)
        K += 1
    else:
        n += 1
    if n == N:
        for i in range(N-K+1):
            print(0)
        K = N+1

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import numpy as np
from collections import Counter

N = int(input())
A = list(map(int,input().split()))

a = Counter(A).values()
B = list(a)
B = np.array(B)
F = [0] * N
for i in range(1,N+1):
    F[i-1] = np.sum(np.minimum(B, i)) / i
num = N
K = 1
while K < N + 1:
    if F[num - 1] >= K:
        print(num)
        K += 1
    else:
        num -= 1
    if num == 0 and K != N + 1:
        for i in range(N-K+1):
            print(0)
        K = N + 1
Problem p02995 (python)

Fixed submission:
# -*- coding: utf-8 -*-
A, B, C, D = map(int, input().split())

ans = 0
def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

CD = (C * D) // gcd(C, D) 

def l(a, x):
    if a // x == 0:
        return 1
    if a % x != 0:
        t = a // x + 1
        return t
    t = a // x
    return t

cl = l(A, C)
dl = l(A, D)
cdl = l(A, CD)
cr = B // C
dr = B // D
cdr = B // (CD)

ans = B - A + 1 - (cr - cl + 1) - (dr - dl + 1) + (cdr - cdl + 1)
        
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math

A, B, C, D = map(int, input().split())

ans = 0

CD = (C * D) // math.gcd(C, D) 

def l(a, x):
    if a // x == 0:
        return 1
    if a % x != 0:
        t = a // x + 1
        return t
    t = a // x
    return t

cl = l(A, C)
dl = l(A, D)
cdl = l(A, CD)
cr = B // C
dr = B // D
cdr = B // (CD)

ans = B - A + 1 - (cr - cl + 1) - (dr - dl + 1) + (cdr - cdl + 1)
        
print(ans)
Problem p03006 (python)

Fixed submission:
from collections import Counter

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

pq = []

for x, y in A:
    for i, j in A:
        if x == i and y == j:
            continue
        pq.append(str(i-x)+str(j-y))

ans = Counter(pq).most_common()

if ans == []:
    print(1)
else:
    print(N - ans[0][1])

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

pq = []

for x, y in A:
    for i, j in A:
        if x == i and y == j:
            continue
        pq.append(str(i-x)+str(j-y))

ans = Counter(pq).most_common()

print(N - ans[0][1])
Problem p03061 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

L = [0] * (N+1)
R = [0] * (N+2)

for i in range(N):
    L[i+1] = gcd(L[i],A[i])
for i in range(N)[::-1]:
    R[i+1] = gcd(R[i+2],A[i])
    
ans = 0

for i in range(1,N+1):
    ans = max(ans, gcd(L[i-1], R[i+1]))

print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd

N = int(input())
A = list(map(int, input().split()))

L = [0] * (N+1)
R = [0] * (N+2)

for i in range(N):
    L[i+1] = gcd(L[i],A[i])
for i in range(N)[::-1]:
    R[i+1] = gcd(R[i+2],A[i])
    
ans = 0

for i in range(1,N+1):
    ans = max(ans, gcd(L[i-1], R[i+1]))

print(ans)
Problem p03074 (python)

Fixed submission:
from itertools import groupby
import sys

N, K = map(int, input().split())
S = input()

S = [[i, len(list(j))] for i, j in groupby(S)]

if len(S) // 2 <= K:
    if len(S) % 2 == 1 and S[-1][0] == "0":
        if len(S) == 1:
            print(sum([j for i, j in S]))
            sys.exit()
            
    else:
        print(sum([j for i, j in S]))
        sys.exit()

ans = 0
cnt = 0
lidx = -1
for i, j in S:
    lidx += 1
    if cnt == K and i == "0":
        break
    ans += j
    if i == "0":
        cnt += 1
sidx = 0
mans = ans
for i in range(lidx,len(S)):
    ans += S[i][1]
    if i == len(S)-1 or S[i][0] == "1":
        while True:
            ans -= S[sidx][1]
            sidx += 1
            if S[sidx][0] == "1":
                break
        mans = max(mans, ans)

print(mans)

Buggy submission (verdict: Runtime Error):
from itertools import groupby
import sys

N, K = map(int, input().split())
S = input()

S = [[i, len(list(j))] for i, j in groupby(S)]

if len(S) // 2 <= K:
    if len(S) % 2 == 1 and S[-1][0] == "0":
        pass
    else:
        print(sum([j for i, j in S]))
        sys.exit()

ans = 0
cnt = 0
lidx = -1
for i, j in S:
    lidx += 1
    if cnt == K and i == "0":
        break
    ans += j
    if i == "0":
        cnt += 1
sidx = 0
mans = ans
for i in range(lidx,len(S)):
    ans += S[i][1]
    if i == len(S)-1 or S[i][0] == "1":
        while True:
            ans -= S[sidx][1]
            sidx += 1
            if S[sidx][0] == "1":
                break
        mans = max(mans, ans)

print(mans)
Problem p03231 (python)

Fixed submission:
N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

Buggy submission (verdict: Runtime Error):
import sys

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

    
print(-20)
sys.exit()


for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Buggy submission (verdict: Runtime Error):
import sys

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]
    
print(-20)
sys.exit()

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Buggy submission (verdict: Runtime Error):
import sys

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Buggy submission (verdict: Runtime Error):
import sys

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N) + 1
    X[index-1] = S[i]

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Buggy submission (verdict: Runtime Error):
import sys

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N) + 1
    X[index-1] = S[i]

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    X[index-1] = T[i]
    
print(L)