Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))
 
N,M = map(int,input().split())
S = input()
T = input()
L = lcm(N,M)
flag = True
min_len = min(Na,M)
s_index = int(int(L/M)/gcd(int(L/M),int(L/N)))
t_index = int(int(L/N)/gcd(int(L/M),int(L/N)))

if N == M:
  for i in range(N):
    if S[i] == T[i]:
      continue
    else:
      flag = False
      break
else:
  for i in range(min_len):
    if i * s_index < N-1 and i*t_index < M - 1:
      if S[i*s_index] == T[i*t_index]:
        continue
      else:
        flag = False
        break
    else:
      break
    
if flag:  
  print(L)
else:
  print(-1)

Fixed submission:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))
 
N,M = map(int,input().split())
S = input()
T = input()
L = lcm(N,M)
flag = True
min_len = min(N,M)
s_index = int(int(L/M)/gcd(int(L/M),int(L/N)))
t_index = int(int(L/N)/gcd(int(L/M),int(L/N)))

if N == M:
  for i in range(N):
    if S[i] == T[i]:
      continue
    else:
      flag = False
      break
else:
  for i in range(min_len):
    if i * s_index < N-1 and i*t_index < M - 1:
      if S[i*s_index] == T[i*t_index]:
        continue
      else:
        flag = False
        break
    else:
      break
    
if flag:  
  print(L)
else:
  print(-1)

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

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))

X = ["" for i in range(lcm(N,M))]
L = len(X)
flag = True

for i in range(N):
  X[(i+1-1)*int(L/N)] = S[i]

for i in range(M):
  if X[(i+1-1)*int(L/M)] == "":
    X[(i+1-1)*int(L/M)] = T[i]
  else:
    if X[(i+1-1)*int(L/M)] == T[i]:
      continue
    else:
      flag = False
      break
      
if flag:  
  print(L)
else:
  print(-1)


Fixed submission:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))
 
N,M = map(int,input().split())
S = input()
T = input()
L = lcm(N,M)
flag = True
min_len = min(N,M)
s_index = int(int(L/M)/gcd(int(L/M),int(L/N)))
t_index = int(int(L/N)/gcd(int(L/M),int(L/N)))

if N == M:
  for i in range(N):
    if S[i] == T[i]:
      continue
    else:
      flag = False
      break
else:
  for i in range(min_len):
    if i * s_index < N-1 and i*t_index < M - 1:
      if S[i*s_index] == T[i*t_index]:
        continue
      else:
        flag = False
        break
    else:
      break
    
if flag:  
  print(L)
else:
  print(-1)

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

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))

X = ["" for i in range(lcm(N,M))]
L = len(X)
flag = True

for i in range(N):
  X[(i+1-1)*int(L/N)] = S[i]

for i in range(M):
  X[(i+1-1)*int(L/M)] = T[i]

for i in range(N):
  if X[(i+1-1)*int(L/N)] == S[i]:
    continue
  else:
    flag = False
    break
if flag:
  for i in range(M):
    if X[(i+1-1)*int(L/M)] == T[i]:
      continue
    else:
      flag == False
      break

if flag:  
  print(L)
else:
  print(-1)

Fixed submission:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))
 
N,M = map(int,input().split())
S = input()
T = input()
L = lcm(N,M)
flag = True
min_len = min(N,M)
s_index = int(int(L/M)/gcd(int(L/M),int(L/N)))
t_index = int(int(L/N)/gcd(int(L/M),int(L/N)))

if N == M:
  for i in range(N):
    if S[i] == T[i]:
      continue
    else:
      flag = False
      break
else:
  for i in range(min_len):
    if i * s_index < N-1 and i*t_index < M - 1:
      if S[i*s_index] == T[i*t_index]:
        continue
      else:
        flag = False
        break
    else:
      break
    
if flag:  
  print(L)
else:
  print(-1)