Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
S = input()
T = input()

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    if ans1[index-1] != '' and ans1[index-1] != T[i]:
        length = -1
        break

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
S = input()
T = input()

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    if ans1[index-1] != '' and ans1[index-1] != T[i]:
        print(-1)
        exit()

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
S = input()
T = input()

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    ans2[index-1] = T[i]

for s, t in zip(ans1, ans2):
    if s != '' and t != '':
        if s != t:
            print(-1)
            exit()

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)

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

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

length = N * M // math.gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

result = ''
for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    ans2[index-1] = T[i]

for s, t in zip(ans1, ans2):
    if s != '' and t != '':
        if s != t:
            print(-1)
            exit()

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)

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

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

length = N * M // gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

result = ''
for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    ans2[index-1] = T[i]

for s, t in zip(ans1, ans2):
    if s != '' and t != '':
        if s != t:
            print(-1)
            exit()

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)

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

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

length = N * M // gcd(N, M)
ans1 = ['']*length
ans2 = ['']*length

result = ''
for i in range(N):
    index = i * length // N + 1
    ans1[index-1] = S[i]

for i in range(M):
    index = i * length // M + 1
    ans2[index-1] = T[i]

for s, t in zip(ans1, ans2):
    if s != '' and t != '':
        if s != t:
            print(-1)
            exit()

print(length)


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

# ユークリッドの互除法
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

length = N * M // gcd(N, M)

if S[0::length//M] == T[0::length//N]:
    print(length)
else:
    print(-1)