Buggy python submission (verdict: Runtime Error):
from fractions import gcd
from bisect import bisect_left

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

L = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(L)

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
S = str(input())
T = str(input())
#print(S)
 
# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a
 
# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)
 
L = int(lcm(N, M))
 
#文字列の作成
#SをもとにX
X1 = r''
n = 0
m = 0
 
#list内包にしたらはやいのかな？
for i in range(0,L):
  if i == n * (L//N):
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる
 
  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      m += 1
    else:
      print(-1)
      exit()
 
print(int(L)) 

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
S = str(input())
T = str(input())
#print(S)
 
# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a
 
# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)
 
L = int(lcm(N, M))
 
#文字列の作成
#SをもとにX
X1 = r''
n = 0
m = 0
 
#list内包にしたらはやいのかな？
for i in range(0,L):
  if i == n * (L//N):
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる
 
  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      m += 1
    else:
      print(-1)
      exit()
 
print(int(L)) 

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

Buggy python submission (verdict: Time Limit Exceeded):
import re

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

# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)

L = int(lcm(N, M))

#文字列の作成
#SをもとにX
X1 = r''
n = 0
m = 0
ans = 0


for i in range(0,int(L)):
  if i == n * (L//N):
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる

  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      ans = 1
      m += 1
    else:
      ans = -1
      break

if ans != -1:
  print(int(L))
else:
  print(-1)

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

Buggy python submission (verdict: Time Limit Exceeded):
import re

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

# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)

L = int(lcm(N, M))

#文字列の作成
#SをもとにX
X1 = r''
n = 0
m = 0
ans = 0


for i in range(0,int(L)):
  if i == n * (L//N):
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる

  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      ans = 1
      m += 1
    else:
      ans = -1
      break

if ans != -1:
  print(int(L))
else:
  print(-1)

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

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

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

# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)

L = int(lcm(N, M))

#文字列の作成
#SをもとにX
X1 = r''
n = 0
for i in range(1,int(L)):
  if i == n * (L//N) + 1:
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる
m = 0
ans = 0
for i in range(0,int(L)):
  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      ans = 1
      m += 1
    else:
      ans = -1
      break

if ans != -1:
  print(int(L))
else:
  print(-1)

Fixed submission:
from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)