Buggy python submission (verdict: Runtime Error):
rom fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p #20ms
tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Runtime Error):
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
# ans=n*m//p
# tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=ord(s[i])
for i in range(m):
	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=ord(s[i])
for i in range(m):
	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!=0:
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!=0:
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
n,m=map(int,input().split())
s=input()
t=input()
p=math.gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!="":
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=input()
t=input()
p=fractions.gcd(n,m)
ans=n*m//p
tt=[""]*ans
for i in range(n):
	tt[i*(ans//n)]=s[i]
print(ans)

Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=input()
t=input()
p=fractions.gcd(n,m)
ans=n*m//p
tt=[""]*ans
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!="":
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)

Buggy python submission (verdict: Runtime Error):
import fractions
n,m=map(int,input().split())
s=input()
t=input()
p=fractions.gcd(n,m)
ans=n*m//p
tt=[""]*ans
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!="":
		print(-1)
		exit()
print(ans)


Fixed submission:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p 
for i in range(n):
	if abs((i*(ans//n)/(ans//m))-int(i*(ans//n)/(ans//m)))<=10**(-7):
		if s[i]==t[(i*(ans//n)//(ans//m))]:
			continue
		else:
			print(-1)
			exit()
print(ans)