Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M

for i in range(lcm_num):
    if i == 0 or (i%(lN) == 0 and i%(lM) == 0):
        if S[i//lN] == T[i//lM]:
            continue
        else:
            print(-1)
            exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M

for i in range(lcm_num):
    if i == 0 or (i%(lN) == 0 and i%(lM) == 0):
        if S[i//lN] == T[i//lM]:
            continue
        else:
            print(-1)
            exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

# X = [0 for i in range(lcm_num)]

for i in range(lcm_num):
    if i == 0 or (i%(lcm_num // N) == 0 and i%(lcm_num // M) == 0):
        if S[i//(lcm_num // N)] == T[i//(lcm_num // M)]:
            continue
        else:
            print(-1)
            exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

X = [0 for i in range(lcm_num)]

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

for i in range(M):
    tmp = i*lcm_num // M + 1
    if X[tmp-1] == 0 or X[tmp-1] == T[i]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

X = ["" for i in range(lcm_num)]

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

for i in range(M):
    tmp = i*lcm_num // M + 1
    if X[tmp-1] == "" or X[tmp-1] == T[i]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

# X_s = ["" for i in range(lcm_num)]
# X_t = ["" for i in range(lcm_num)]
X = ["" for i in range(lcm_num)]

# print(X_s,X_t)

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


for i in range(M):
    tmp = i*lcm_num // M + 1
    if X[tmp-1] == "" or X[tmp-1] == T[i]:
        pass
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

# X_s = ["" for i in range(lcm_num)]
# X_t = ["" for i in range(lcm_num)]
X = ["" for i in range(lcm_num)]

# print(X_s,X_t)

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


for i in range(M):
    tmp = i*lcm_num // M + 1
    if X[tmp-1] == "" or X[tmp-1] == T[i]:
        pass
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)

X_s = ["" for i in range(lcm_num)]
X_t = ["" for i in range(lcm_num)]

# print(X_s,X_t)

for i in range(N):
    tmp = i*lcm_num // N + 1
    X_s[tmp-1] = S[i]


for i in range(M):
    tmp = i*lcm_num // M + 1
    X_t[tmp-1] = T[i]

# print(X_s,X_t)

for s,t in zip(X_s,X_t):
    if s == "" or t == "" :
        continue
    elif s == t:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Runtime Error):
import fraction
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

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

lcm_num = lcm(N,M)

X_s = ["" for i in range(lcm_num)]
X_t = ["" for i in range(lcm_num)]

# print(X_s,X_t)

for i in range(N):
    tmp = i*lcm_num // N + 1
    X_s[tmp-1] = S[i]


for i in range(M):
    tmp = i*lcm_num // M + 1
    X_t[tmp-1] = T[i]

# print(X_s,X_t)

for s,t in zip(X_s,X_t):
    if s == "" or t == "" :
        continue
    elif s == t:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

Buggy python submission (verdict: Runtime Error):
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

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

lcm_num = lcm(N,M)

X_s = ["" for i in range(lcm_num)]
X_t = ["" for i in range(lcm_num)]

# print(X_s,X_t)

for i in range(N):
    tmp = i*lcm_num // N + 1
    X_s[tmp-1] = S[i]


for i in range(M):
    tmp = i*lcm_num // M + 1
    X_t[tmp-1] = T[i]

# print(X_s,X_t)

for s,t in zip(X_s,X_t):
    if s == "" or t == "" :
        continue
    elif s == t:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

def lcm(x, y):
    return (x * y) // gcd(x, y)

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)