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

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()
try:
    L = int(lcm(N, M))
except:
    print('yy')
    exit()
i = 1
while True:
    flag = True
    L_ = L * i
    a = [None] * L_
    for j in range(N):
        a[(L_ // N) * j] = S[j]
    
    for j in range(M):
        if a[(L_ // M) * j] is not None:
            if a[(L_ // M ) * j] != T[j]:
                flag = False
                break

    if flag:
        print(L_)
        exit()

    i += 1

Fixed submission:
import fractions

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()

L = int(lcm(N, M))
print(L)

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

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

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

d_N = {}
try:
    for n in range(N):
        d = fractions.gcd(n, N)
        d_N[(n // d, N // d)] = n

    for m in range(M):
        d = fractions.gcd(m, M)
        k = (m // d, M // d)
        if k in d_N:
            if S[d_N[k]] != T[m]:
                print(-1)
                exit()
except:
    print('AAA')
    exit()

L = int(lcm(N, M))
i = 1
while True:
    flag = True
    L_ = L * i
    a = [None] * L_
    for j in range(N):
        a[(L_ // N) * j] = S[j]
    
    for j in range(M):
        if a[(L_ // M) * j] is not None:
            if a[(L_ // M )* j] != T[j]:
                flag = False
                break

    if flag:
        print(L_)
        exit()

    i += 1

Fixed submission:
import fractions

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()

L = int(lcm(N, M))
print(L)

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

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()

L = int(lcm(N, M))
i = 1
while True:
    flag = True
    L_ = L * i
    a = ['0'] * L_
    for j in range(N):
        a[L_ // N * j] = S[j]
    
    for j in range(M):
        if (a[L_ // M * j] != '0') and (a[L_ // M * j] != T[j]):
            flag = False
            break

    if flag:
        print(L_)
        exit()

    i += 1
            







Fixed submission:
import fractions

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()

L = int(lcm(N, M))
print(L)

Buggy python submission (verdict: Runtime Error):
import fractions
from collections import defaultdict

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print('No')
            exit()

L = int(lcm(N, M))
i = 1
while True:
    flag = True
    L_ = L * i
    a = ['0'] * L_
    for j in range(N):
        a[L_ // N * j] = S[j]
    
    for j in range(M):
        if (a[L_ // M * j] != '0') and (a[L_ // M * j] != T[j]):
            flag = False
            break
        else:
            a[L_ // M * j] = T[j]

    if flag:
        print(L_)
        exit()

    i += 1
            







Fixed submission:
import fractions

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

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

d_N = {}

for n in range(N):
    d = fractions.gcd(n, N)
    d_N[(n // d, N // d)] = n

for m in range(M):
    d = fractions.gcd(m, M)
    k = (m // d, M // d)
    if k in d_N:
        if S[d_N[k]] != T[m]:
            print(-1)
            exit()

L = int(lcm(N, M))
print(L)