import fractions

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

S = eval(input())

T = eval(input())

g = fractions.gcd(N,M)

lcm = N*M // g



for i in range(g):

    a = S[N//g * i]

    b = T[M//g * i]

    if a != b:

        print((-1))

        exit()

print(lcm)
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))
import fractions

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

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))

-import fractions +from fractions import gcd -g = fractions.gcd(N, M) -lcm = N * M // g -for i in range(g): -    a = S[N // g * i] -    b = T[M // g * i] -    if a != b: -        print((-1)) -        exit() -print(lcm) +g = gcd(N, M) +ans = N * M // g +print((ans if S[:: N // g] == T[:: M // g] else -1))