from collections import defaultdict

from fractions import gcd

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

S = eval(input())

T = eval(input())

L = N * M // gcd(N, M)

ds = L // N

dt = L // M

dict_s = defaultdict(str)

dict_t = defaultdict(str)



for i in range(N):

  dict_s[i*ds] = S[i]

  

for i in range(M):

  dict_t[i*dt] = T[i]



for key in list(dict_s.keys()):

  if key in list(dict_t.keys()):

    if dict_s[key] != dict_t[key]:

      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)

lcm = N * M // g

for i in range(g):

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

    print((-1))

    exit()

print(lcm)
from collections import defaultdict
from fractions import gcd

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
L = N * M // gcd(N, M)
ds = L // N
dt = L // M
dict_s = defaultdict(str)
dict_t = defaultdict(str)
for i in range(N):
    dict_s[i * ds] = S[i]
for i in range(M):
    dict_t[i * dt] = T[i]
for key in list(dict_s.keys()):
    if key in list(dict_t.keys()):
        if dict_s[key] != dict_t[key]:
            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)
lcm = N * M // g
for i in range(g):
    if S[i * N // g] != T[i * M // g]:
        print((-1))
        exit()
print(lcm)

-from collections import defaultdict -L = N * M // gcd(N, M) -ds = L // N -dt = L // M -dict_s = defaultdict(str) -dict_t = defaultdict(str) -for i in range(N): -    dict_s[i * ds] = S[i] -for i in range(M): -    dict_t[i * dt] = T[i] -for key in list(dict_s.keys()): -    if key in list(dict_t.keys()): -        if dict_s[key] != dict_t[key]: -            print((-1)) -            exit() -print(L) +g = gcd(N, M) +lcm = N * M // g +for i in range(g): +    if S[i * N // g] != T[i * M // g]: +        print((-1)) +        exit() +print(lcm)