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

    G = math.gcd(N, M)
    L = (N * M) // G

    for i in range(G):
        if S[i * N // G] != T[i * M // G]:
            print(-1)
            exit()

    print(L)

if __name__ == "__main__":
    main4()

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

    L = (N * M) // math.gcd(N, M)

    pair = dict()

    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        pair[idx] = S[i - 1]

    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if idx in pair.keys():
            if pair[idx] != T[i - 1]:
                print(-1)
                exit()

    print(L)

if __name__ == "__main__":
    main4()

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main3():
    N, M = map(int, input().split())
    S = input()
    T = input()

    L = (N * M) // math.gcd(N, M)
    good = [-1 for i in range(4 * L)]

    idx_s = 0
    idx_t = 0

    for i in range(1, max(N, M) + 1):
        idx_s = (i - 1) * (L // N) + 1
        idx_t = (i - 1) * (L // M) + 1

        if N < M:

            if i <= N:
                good[idx_s] = S[i - 1]

            if i == 1:
                if S[i - 1] != T[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_t] == -1 or good[idx_t] == T[i - 1]:
                    good[idx_t] = T[i - 1]
                else:
                    print(-1)
                    exit()

        else: #N >= M

            if i <= M:
                good[idx_t] = T[i - 1]

            if i == 1:
                if T[i - 1] != S[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_s] == -1 or good[idx_s] == S[i - 1]:
                    good[idx_s] = S[i - 1]
                else:
                    print(-1)
                    exit()

    max_idx = max(((N - 1) * (L // N) + 1), ((M - 1) * (L // M) + 1))
    r, m = divmod(max_idx, L)

    ans = 0
    if m == 0:
        ans = r * L
    else:
        ans = (r + 1) * L

    print(ans)

if __name__ == "__main__":
    main3()

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

    L = (N * M) // math.gcd(N, M)

    pair = dict()

    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        pair[idx] = S[i - 1]

    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if idx in pair.keys():
            if pair[idx] != T[i - 1]:
                print(-1)
                exit()

    print(L)

if __name__ == "__main__":
    main4()

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main3():
    N, M = map(int, input().split())
    S = input()
    T = input()

    L = (N * M) // math.gcd(N, M)
    good = [-1 for i in range(4 * L)]

    idx_s = 0
    idx_t = 0

    for i in range(1, max(N, M) + 1):
        idx_s = (i - 1) * (L // N) + 1
        idx_t = (i - 1) * (L // M) + 1

        if N < M:

            if i <= N:
                good[idx_s] = S[i - 1]

            if i == 1:
                if S[i - 1] != T[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_t] == -1 or good[idx_t] == T[i - 1]:
                    good[idx_t] = T[i - 1]
                else:
                    print(-1)
                    exit()

        else: #N >= M

            if i <= M:
                good[idx_t] = T[i - 1]

            if i == 1:
                if T[i - 1] != S[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_s] == -1 or good[idx_s] == S[i - 1]:
                    good[idx_s] = S[i - 1]
                else:
                    print(-1)
                    exit()

    max_idx = max(((N - 1) * (L // N) + 1), ((M - 1) * (L // M) + 1))
    r, m = divmod(max_idx, L)

    ans = 0
    if m == 0:
        ans = r * L
    else:
        ans = (r + 1) * L

    print(ans)

if __name__ == "__main__":
    main3()

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

    L = (N * M) // math.gcd(N, M)

    pair = dict()

    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        pair[idx] = S[i - 1]

    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if idx in pair.keys():
            if pair[idx] != T[i - 1]:
                print(-1)
                exit()

    print(L)

if __name__ == "__main__":
    main4()

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main():
    N, M = map(int, input().split())
    S = input()
    T = input()

    L = (N * M) // math.gcd(N, M)
    good = [-1 for i in range(4 * L)]

    idx_s = 0
    idx_t = 0

    idx = 0
    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        good[idx] = S[idx_s]
        idx_s += 1

    idx = 0
    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if i == 1:
            if good[idx] != T[idx_t]:
                print(-1)
                exit()
            else:
                idx_t += 1

        else:
            if good[idx] == -1 or good[idx] == T[idx_t]:
                good[idx] = T[idx_t]
                idx_t += 1
            else:
                print(-1)
                exit()
    
    max_idx = max(((N - 1) * (L // N) + 1), ((M - 1) * (L // M) + 1))
    r, m = divmod(max_idx, L)

    ans = 0
    if m == 0:
        ans = r * L
    else:
        ans = (r + 1) * L

    print(ans)

if __name__ == "__main__":
    main()

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

    L = (N * M) // math.gcd(N, M)

    pair = dict()

    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        pair[idx] = S[i - 1]

    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if idx in pair.keys():
            if pair[idx] != T[i - 1]:
                print(-1)
                exit()

    print(L)

if __name__ == "__main__":
    main4()