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)
n, m = list(map(int,input().split()))

s = eval(input())

t = eval(input())

def gcd(a, b):

    while b > 0:

        a, b = b, a%b

    return a

g = gcd(n, m)

l = n * m // g

for i in range(0, g):

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

    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)

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


def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


g = gcd(n, m)
l = n * m // g
for i in range(0, g):
    if s[i * n // g] != t[i * m // g]:
        print("-1")
        exit()
print(l)

-import fractions - -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]: + + +def gcd(a, b): +    while b > 0: +        a, b = b, a % b +    return a + + +g = gcd(n, m) +l = n * m // g +for i in range(0, g): +    if s[i * n // g] != t[i * m // g]: