from fractions import gcd





def lcm(x, y):

    return x // gcd(x, y) * y





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

S = eval(input())

T = eval(input())



L = lcm(N, M)

X = {}



for i in range(N):

    X[(L // N) * i] = S[i]



for i in range(M):

    if (L // M) * i in X and X[(L // M) * i] != T[i]:

        print((-1))

        exit()

print(L)

from fractions import gcd





def lcm(x, y):

    return x // gcd(x, y) * y





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

S = eval(input())

T = eval(input())



for i in range(N):

    if M * i % N == 0 and S[i] != T[M * i // N]:

        print((-1))

        exit()

print((lcm(N, M)))
from fractions import gcd


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


N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
L = lcm(N, M)
X = {}
for i in range(N):
    X[(L // N) * i] = S[i]
for i in range(M):
    if (L // M) * i in X and X[(L // M) * i] != T[i]:
        print((-1))
        exit()
print(L)

from fractions import gcd


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


N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
for i in range(N):
    if M * i % N == 0 and S[i] != T[M * i // N]:
        print((-1))
        exit()
print((lcm(N, M)))

-L = lcm(N, M) -X = {} -    X[(L // N) * i] = S[i] -for i in range(M): -    if (L // M) * i in X and X[(L // M) * i] != T[i]: +    if M * i % N == 0 and S[i] != T[M * i // N]: -print(L) +print((lcm(N, M)))