Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

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

dic = {}
L = int((N*M) / gcd(N,M))
ans = L

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

for i in range(M):
  if i*(L/M)+1 in dic.keys():
    if S[dic[int(i*(L/M)+1)]] == T[i]:continue
    else: 
      ans = -1
      break

  


print(ans)


Fixed submission:
from fractions import gcd

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

dic = {}
L = int((N*M) / gcd(N,M))
ans = L

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

key = set(dic.keys())
for i in range(M):
  if i*(L/M)+1 in key:
    if S[dic[int(i*(L/M)+1)]] == T[i]:continue
    else: 
      ans = -1
      break

  


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

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


dic = {i+1: [] for i in range(int((N*M) / gcd(N,M)))}
L = len(dic)
ans = L

for n in range(N):
  k = int(n*(L/N))+1
  #print(k)
  dic[k].append(S[n])

#print(dic)
for m in range(M):
  k = int(m * (L/M) + 1)
  if len(dic[k]) == 0:continue
  else: 
    #print(T[m],dic[k])
    if dic[k][0] != T[m]:
      ans = -1
      break


print(ans)


Fixed submission:
from fractions import gcd

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

dic = {}
L = int((N*M) / gcd(N,M))
ans = L

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

key = set(dic.keys())
for i in range(M):
  if i*(L/M)+1 in key:
    if S[dic[int(i*(L/M)+1)]] == T[i]:continue
    else: 
      ans = -1
      break

  


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

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


dic = {i+1: [] for i in range(int((N*M) / gcd(N,M)))}
L = len(dic)
ans = L

for n in range(N):
  k = int(n*(L/N))+1
  #print(k)
  dic[k].append(S[n])

#print(dic)
for m in range(M):
  k = int(m * (L/M) + 1)
  if len(dic[k]) == 0:continue
  else: 
    #print(T[m],dic[k])
    if dic[k][0] != T[m]:
      ans = -1
      break


print(ans)


Fixed submission:
from fractions import gcd

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

dic = {}
L = int((N*M) / gcd(N,M))
ans = L

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

key = set(dic.keys())
for i in range(M):
  if i*(L/M)+1 in key:
    if S[dic[int(i*(L/M)+1)]] == T[i]:continue
    else: 
      ans = -1
      break

  


print(ans)

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

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


dic = {i+1: [] for i in range(int((N*M) / math.gcd(N,M)))}
L = len(dic)
ans = L

for n in range(N):
  k = int(n*(L/N))+1
  #print(k)
  dic[k].append(S[n])

#print(dic)
for m in range(M):
  k = int(m * (L/M) + 1)
  if len(dic[k]) == 0:continue
  else: 
    #print(T[m],dic[k])
    if dic[k][0] != T[m]:
      ans = -1
      break


print(ans)


Fixed submission:
from fractions import gcd

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

dic = {}
L = int((N*M) / gcd(N,M))
ans = L

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

key = set(dic.keys())
for i in range(M):
  if i*(L/M)+1 in key:
    if S[dic[int(i*(L/M)+1)]] == T[i]:continue
    else: 
      ans = -1
      break

  


print(ans)