Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]+["end"]
TT=[j*t for j in range(M)]+["end"]
p,q=0,0
ans=N*s
for k in range(N*s):
    if SS[p]==k:
        if TT[q]==k:
            if S[p]!=T[q]:
                ans=-1
        p+=1
    if TT[q]==k:
        q+=1
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]+["end"]
TT=[j*t for j in range(M)]+["end"]
p,q=0,0
ans=N*s
for k in range(N*s):
    if SS[p]==k:
        if TT[q]==k:
            if S[p]!=T[q]:
                ans=-1
        p+=1
    if TT[q]==k:
        q+=1
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
for k in range(N*s):
    if k==SS[p]:
        if k==TT[q]:
            if S[p]!=T[q]:
                ans=-1
        p+=1
    if k==TT[q]:
        q+=1
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
for k in range(N*s):
    if k in SS:
        if k in TT:
            if S[p]!=T[q]:
                ans=-1
        p+=1
    if k in TT:
        q+=1
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=M//gcd(N,M)
t=N//gcd(N,M)
if s==1:SS=S
else:SS=("?"*(s-1)).join(list(S))+"?"*(s-1)
if t==1:TT=T
else:TT=("?"*(t-1)).join(list(T))+"?"*(t-1)
for i in range(len(SS)):
    if SS[i]==TT[i] or SS[i]=="?" or TT[i]=="?":pass
    else:
        print(-1)
        exit()
print(len(SS))

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=M//gcd(N,M)
t=N//gcd(N,M)
if s==1:SS=S
else:SS=("?"*(s-1)).join(list(S))+"?"*(s-1)
if t==1:TT=T
else:TT=("?"*(t-1)).join(list(T))+"?"*(t-1)
for i in range(len(SS)):
    if SS[i]==TT[i] or SS[i]=="?" or TT[i]=="?":pass
    else:
        print(-1)
        exit()
print(len(SS))

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
s=(M//gcd(N,M))
t=(N//gcd(N,M))
SS=[i*s for i in range(N)]
TT=[j*t for j in range(M)]
p,q=0,0
ans=N*s
while True:
    if SS[p]==TT[q]:
        if S[p]!=T[q]:ans=-1
        p+=1
        q+=1
    elif SS[p]>TT[q]:
        q+=1
    else:
        p+=1
    if p==N or q==M:
        break
print(ans)