Buggy python submission (verdict: Runtime Error):
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    size = lcm(m, n)
    X = [None] * (size + 1000)
    S = input()
    T = input()
    try:
        for i in range(n):
            X[i * (size // n) + 1] = S[i]
        for i in range(m):
            if X[i * (size // m) + 1] == T[i]:
                continue
            elif not X[i * (size // m) + 1]:
                X[i * (size // m) + 1] = T[i]
            else:
                print(-1)
                return
        print(size)
    except BaseException:
        print(-1)
    return


if __name__ == "__main__":
    main()


Fixed submission:
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    _gcd = gcd(m, n)
    size = lcm(m, n)
    S = input()
    T = input()
    if S[::n // _gcd] == T[::m // _gcd]:
        print(size)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    size = lcm(m, n)
    X = [None] * (size + 1)
    S = input()
    T = input()
    for i in range(n):
        X[i * (size // n) + 1] = S[i]
    for i in range(m):
        if X[i * (size // m) + 1] == T[i]:
            continue
        elif not X[i * (size // m) + 1]:
            X[i * (size // m) + 1] = T[i]
        else:
            print(-1)
            return
    print(size)
    return


if __name__ == "__main__":
    main()


Fixed submission:
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    _gcd = gcd(m, n)
    size = lcm(m, n)
    S = input()
    T = input()
    if S[::n // _gcd] == T[::m // _gcd]:
        print(size)
    else:
        print(-1)


if __name__ == "__main__":
    main()