Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
Answer = int(N*M/x)

X = [0 for i in range(Answer)]

for i in range(N):
    X[int(i*Answer/N)] = S[i]

for i in range(M):
    if (X[int(i*Answer/M)] == 0)or(X[int(i*Answer/M)] == T[i]):
        continue
    else:
        print(-1)
        exit()
print(Answer)


Fixed submission:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)

for i in range(x):
    if S[int(i*y/M)] == T[int(i*y/N)]:
        continue
    else:
        print(-1)
        exit()
print(y)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
x = gcd(len(S),len(T))
Answer = int(len(S)*len(T)/x)
X = [0 for i in range(Answer)]

if (x == 1)and(S[0] == T[0]):
    print(Answer)
    exit()

for i in range(len(S)):
    X[int(i*Answer/len(S))] = S[i]

for i in range(len(T)):
    if (X[int(i*Answer/len(T))] == 0)or(X[int(i*Answer/len(T))] == T[i]):
        continue
    else:
        print(-1)
        exit()
print(Answer)


Fixed submission:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)

for i in range(x):
    if S[int(i*y/M)] == T[int(i*y/N)]:
        continue
    else:
        print(-1)
        exit()
print(y)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
Answer = int(len(S)*len(T)/gcd(len(S),len(T)))
X = [0 for i in range(Answer)]

if (gcd(len(S),len(T)) == 1)and(S[0] == T[0]):
    print(Answer)
    exit()

for i in range(len(S)):
    X[int(i*Answer/len(S))] = S[i]

for i in range(len(T)):
    if (X[int(i*Answer/len(T))] == 0)or(X[int(i*Answer/len(T))] == T[i]):
        continue
    else:
        print(-1)
        exit()
print(Answer)


Fixed submission:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)

for i in range(x):
    if S[int(i*y/M)] == T[int(i*y/N)]:
        continue
    else:
        print(-1)
        exit()
print(y)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
Answer = int(len(S)*len(T)/gcd(len(S),len(T)))
X = [0 for i in range(Answer)]

for i in range(len(S)):
    X[int(i*Answer/len(S))] = S[i]

for i in range(len(T)):
    if (X[int(i*Answer/len(T))] == 0)or(X[int(i*Answer/len(T))] == T[i]):
        continue
    else:
        print(-1)
        exit()
print(Answer)


Fixed submission:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)

for i in range(x):
    if S[int(i*y/M)] == T[int(i*y/N)]:
        continue
    else:
        print(-1)
        exit()
print(y)