Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
s, t = list(input()), list(input())


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


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


l = lcm(len(s), len(t))
x = ["無"] * l
for i in range(0, len(s)):
    x[i * (l // len(s))] = s[i]
for i in range(0, len(t)):
    if x[i * (l // len(t))] == "無":
        continue
    else:
        if x[i * (l // len(t))] != t[i]:
            print(-1)
            exit()
print(l)

Fixed submission:
# たぶん最小の値は最小公倍数になりそう
# 内包表記 -> TLE
# [0]*l -> RE

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = {}

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if x in arr and arr[x] != t[i]:
        print(-1)
        exit()
print(l)

Buggy python submission (verdict: Runtime Error):
# たぶん最小の値は最小公倍数になりそう

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = [0] * l

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if arr[x] != 0 and arr[x] != t[i]:
        print(-1)
        exit()
print(l)

Fixed submission:
# たぶん最小の値は最小公倍数になりそう
# 内包表記 -> TLE
# [0]*l -> RE

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = {}

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if x in arr and arr[x] != t[i]:
        print(-1)
        exit()
print(l)

Buggy python submission (verdict: Time Limit Exceeded):
# たぶん最小の値は最小公倍数になりそう

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = [0 for _ in range(l)]

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if arr[x] != 0 and arr[x] != t[i]:
        print(-1)
        exit()
print(l)

Fixed submission:
# たぶん最小の値は最小公倍数になりそう
# 内包表記 -> TLE
# [0]*l -> RE

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = {}

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if x in arr and arr[x] != t[i]:
        print(-1)
        exit()
print(l)

Buggy python submission (verdict: Time Limit Exceeded):
# gcdより長いのは存在しなさそう（勘）なので存在するとしたらgcd
# 実際に試していく
n, m = map(int, input().split())
s, t = input(), input()


def lcm(a, b):
    if a > b:
        return lcm(b, a)
    while 0 != a:
        a, b = b % a, a
    return b


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


u = gcd(n, m)
v = [None for _ in range(u)]
x1 = 0
while x1 < n:
    p = x1 * (u // n) + 1
    # print("p = {}".format(p))
    # if v[p - 1] == None:
    #     v[p - 1] = s[x1]
    # else:
    #     print("すでに入ってるのでおしまい")
    v[p - 1] = s[x1]
    x1 += 1
x2 = 0
while x2 < m:
    p = x2 * (u // m) + 1
    if v[p - 1] == None:
        if x2 > 0:
            v[p - 1] = t[x2]
        else:
            if s[0] == t[0]:
                continue
            else:
                print(-1)
                exit()
    else:
        if v[p - 1] != t[x2]:
            print(-1)
            exit()
    x2 += 1
print(u)


Fixed submission:
# たぶん最小の値は最小公倍数になりそう
# 内包表記 -> TLE
# [0]*l -> RE

n, m = map(int, input().split())
s, t = input(), input()


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


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


l = lcm(n, m)
arr = {}

for i in range(n):
    x = i * (l // n)
    arr[x] = s[i]

for i in range(m):
    x = i * (l // m)
    if x in arr and arr[x] != t[i]:
        print(-1)
        exit()
print(l)