Buggy python submission (verdict: Time Limit Exceeded):
import sys

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

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

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

L = lcm(N, M)
X = [0 for i in range(L)]

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != 0 and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

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

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

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

L = lcm(N, M)
X = ["A" for i in range(L)]

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)


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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

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

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

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

L = lcm(N, M)
X = ["A" for i in range(L)]

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)


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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

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

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

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

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

L = lcm(N, M)
X = ["A"] * L

    
print(-20)
sys.exit()


for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)


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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

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

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

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

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

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]
    
print(-20)
sys.exit()

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)


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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

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

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

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

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

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

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

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

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

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

L = lcm(N, M)
X = ["A"] * L

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

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)

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

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

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

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

L = lcm(N, M)
X = ["A"] * L

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

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    X[index-1] = T[i]
    
print(L)

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

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

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

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)