from fractions import gcd

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

S = eval(input())

T = eval(input())

g = gcd(N,M)

ans = N*M//g

print((ans if S[::N//g] == T[::M//g] else -1))
from fractions import gcd

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

S = eval(input())

T = eval(input())

g = gcd(N,M)

l = N*M//g



for i in range(g):

    if S[N*i//g] != T[M*i//g]:

        print((-1))

        exit()

print(l)
from fractions import gcd

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
g = gcd(N, M)
ans = N * M // g
print((ans if S[:: N // g] == T[:: M // g] else -1))

from fractions import gcd

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

-ans = N * M // g -print((ans if S[:: N // g] == T[:: M // g] else -1)) +l = N * M // g +for i in range(g): +    if S[N * i // g] != T[M * i // g]: +        print((-1)) +        exit() +print(l)