Buggy python submission (verdict: Time Limit Exceeded):
import fractions
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Fixed submission:
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Buggy python submission (verdict: Time Limit Exceeded):
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Fixed submission:
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M = map(int,input().split())
S,T = [input() for i in range(2)]
for i in range(N):
  for j in range(M):
    if i*M == j*N and S[i] != T[j]:
        print(-1)
        exit()
    elif i == N-1 and j == M-1:
      print(int(N*M/gcd(N,M)))
      
      

Fixed submission:
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M = map(int,input().split())
S,T = [input() for i in range(2)]
for i in range(N):
  for j in range(M):
    if i*M == j*N and S[i] != T[j]:
        print(-1)
        exit()
    elif i == N-1 and j == M-1:
      print(int(N*M/gcd(N,M)))
      
      

Fixed submission:
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
N,M = map(int,input().split())
S,T = [input() for i in range(2)]
for i in range(1,N+1):
  for j in range(1,M+1):
    if (i-1)*M == (j-1)*N and S[i-1] != T[j-1]:
        print(-1)
        exit()
    elif i == N and j == M:
      print(int(N*M/gcd(N,M)))
      
      

Fixed submission:
import fractions
N,M = map(int,input().split())
S = input(); T = input()
gcd = fractions.gcd(N,M)
N_prime = N //gcd; M_prime = M // gcd
for i in range(0,N,N_prime):
    for j in range(0,M,M_prime):
        if i * M_prime - j * N_prime == 0 and S[i] != T[j]:
            print(-1)
            exit()
print(N*M // gcd)