Buggy python submission (verdict: Runtime Error):
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//f.gcd(n,m))
d=(n*m)//f.gcd(n,m)
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(d)
else:
    print(-1)
#print(cnt)



Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//fractions.gcd(n,m))
d=(n*m)//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print(d)
else:
    print(-1)
#print(cnt)

Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//fractions.gcd(n,m))
d=(n*m)//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print((n*m)//fractions.gcd(n,m))
else:
    print(-1)
#print(cnt)

Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*(n*m//fractions.gcd(n,m))
d=n*m//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(-1)
print(cnt)

Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*(n*m//fractions.gcd(n,m))
d=n*m//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(ans)
#print(cnt)

Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0

cnt=[""]*(n*m//fractions.gcd(n,m))
for i in range(n):
    cnt[i*max(n,m)//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*max(n,m)//m]:
        ans=-1
        break
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(ans)

Fixed submission:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(n*m//f.gcd(n,m))
else:
    print(-1)
#print(cnt)