from fractions import gcd

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

S=list(eval(input()))

T=list(eval(input()))

L=gcd(N,M)

print((N*M//L if all([S[i*N//L]==T[i*M//L] for i in range(L)]) else "-1"))
from fractions import gcd



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

S = list(eval(input()))

T = list(eval(input()))



gc = gcd(N, M)



ans = N*M//gc



for i in range(gc):

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

        ans = -1

        break

                

print(ans)
from fractions import gcd

N, M = list(map(int, input().split()))
S = list(eval(input()))
T = list(eval(input()))
L = gcd(N, M)
print((N * M // L if all([S[i * N // L] == T[i * M // L] for i in range(L)]) else "-1"))

from fractions import gcd

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

-L = gcd(N, M) -print((N * M // L if all([S[i * N // L] == T[i * M // L] for i in range(L)]) else "-1")) +gc = gcd(N, M) +ans = N * M // gc +for i in range(gc): +    if S[N // gc * i] != T[M // gc * i]: +        ans = -1 +        break +print(ans)