Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
ans=L


U=[]
V=[]

#1個目
for i in range(N):
  U.append( [i*(L//N),S[i]])
#2個目
for i in range(M):
  V.append( [i*(L//M),T[i]])
#print(U)
#print(V)

for i in U:
  for j in V:
    #print(i,j)
    if i[0]==j[0] and i[1]!=j[1]:
      print(-1)
      exit()


#  U[i*(L//N)]=S[i]
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
ans=L
L=N*M//gcd(N,M)

U=[]
V=[]

#1個目
for i in range(N):
  U.append( [i*(L//N),S[i]])
#2個目
for i in range(M):
  V.append( [i*(L//M),T[i]])
#print(U)
#print(V)
for i in U:
  for j in V:
    if i[0]==j[0] and i[0]!=j[1]:
      print(-1)
      exit()


#  U[i*(L//N)]=S[i]
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
ans=L
L=N*M//gcd(N,M)

U=[]
V=[]

#1個目
for i in range(N):
  U.append( [i*(L//N),S[i]])
#2個目
for i in range(M):
  V.append( [i*(L//M),T[i]])
#print(U)
#print(V)
for i in U:
  for j in V:
    if i[0]==j[0] and i[0]!=j[1]:
      print(-1(
      exit


#  U[i*(L//N)]=S[i]
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()

L=N*M//gcd(N,M)
#print(L)
U=["#" for _ in range(L)]
ans=L

for i in range(N):
  U[i*(L//N)]=S[i]
#print(U)

for j in range(M):
  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
    ans=-1
    break
  else:
    U[j*(L//M)]=T[j]

#print(U)
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()

L=N*M//gcd(N,M)
#print(L)
U=["#" for _ in range(L)]
ans=L

for i in range(N):
  U[i*(L//N)]=S[i]
#print(U)

for j in range(M):
  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
    ans=-1
    break
  else:
    U[j*(L//M)]=T[j]

#print(U)
print(ans)

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
import math

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

t=math.gcd(N,M)

if t==1:
  print(N*M)
elif t==min(N,M):
  print("-1")
elif:
  print(t*max(N,M))

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
import math

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

t=math.gcd(N,M)

if t==1:
  print(N*M)
elif t==min(N,M):
  print(-1)
elif:
  print(t*max(N,M))

Fixed submission:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
p=[]
q=[]

if S[::L//M]==T[::L//N]:
  print(L)
else:
  print(-1)