import fractions

n, m = list(map(int,input().split()))

s = eval(input())

t = eval(input())

q = fractions.gcd(n, m)

l = n * m // q #lcm(n, m)

p = int(l/n * l/m / fractions.gcd(l/n, l/m)) #lcm(l/n, l/m)

for i in range(0, int(l/p)):

  if s[i * p//(l//n)] != t[i * p//(l//m)]:

    print('-1')

    exit()

print(l)
import fractions

n, m = list(map(int,input().split()))

s = eval(input())

t = eval(input())

q = fractions.gcd(n, m)

l = n * m // q #lcm(n, m)

for i in range(0, q):

  if s[i * n//q] != t[i * m//q]:

    print('-1')

    exit()

print(l)
import fractions

n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
q = fractions.gcd(n, m)
l = n * m // q  # lcm(n, m)
p = int(l / n * l / m / fractions.gcd(l / n, l / m))  # lcm(l/n, l/m)
for i in range(0, int(l / p)):
    if s[i * p // (l // n)] != t[i * p // (l // m)]:
        print("-1")
        exit()
print(l)

import fractions

n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
q = fractions.gcd(n, m)
l = n * m // q  # lcm(n, m)
for i in range(0, q):
    if s[i * n // q] != t[i * m // q]:
        print("-1")
        exit()
print(l)

-p = int(l / n * l / m / fractions.gcd(l / n, l / m))  # lcm(l/n, l/m) -for i in range(0, int(l / p)): -    if s[i * p // (l // n)] != t[i * p // (l // m)]: +for i in range(0, q): +    if s[i * n // q] != t[i * m // q]: