from fractions import *



def lcm(a,b): 

    return int(abs(a * b) / gcd(a,b) if a and b else 0)



def getDivisors(a,b):

    divisors = []

    for i in range(2,min(a,b)+1):

        if a % i == 0 and b % i == 0:

            divisors.append(i)

    return divisors



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

a = eval(input())

b = eval(input())



if a[0] != b[0]:

    print((-1))

elif gcd(N,M) == 1:

    print((lcm(N,M)))

elif N != M:

    divisors = getDivisors(N,M)



    for i in divisors:

        k = N//i

        l = M//i

        while k < N and l < M:

            if a[k] != b[l]:

                print((-1))

                quit()

            k += N//i

            l += M//i

    print((lcm(N,M)))

else:

    if a == b: print(N)

    else: print((-1))





from fractions import gcd



def lcm(a,b): 

    return int(abs(a * b) / gcd(a,b) if a and b else 0)



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

S = eval(input())

T = eval(input())



g = gcd(N,M)

n = N // g

m = M // g



for k in range(g):

    if S[k * n] != T[k * m]:

        print((-1))

        quit()

print((lcm(N,M)))
from fractions import *


def lcm(a, b):
    return int(abs(a * b) / gcd(a, b) if a and b else 0)


def getDivisors(a, b):
    divisors = []
    for i in range(2, min(a, b) + 1):
        if a % i == 0 and b % i == 0:
            divisors.append(i)
    return divisors


N, M = list(map(int, input().split()))
a = eval(input())
b = eval(input())
if a[0] != b[0]:
    print((-1))
elif gcd(N, M) == 1:
    print((lcm(N, M)))
elif N != M:
    divisors = getDivisors(N, M)
    for i in divisors:
        k = N // i
        l = M // i
        while k < N and l < M:
            if a[k] != b[l]:
                print((-1))
                quit()
            k += N // i
            l += M // i
    print((lcm(N, M)))
else:
    if a == b:
        print(N)
    else:
        print((-1))

from fractions import gcd


def lcm(a, b):
    return int(abs(a * b) / gcd(a, b) if a and b else 0)


N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
g = gcd(N, M)
n = N // g
m = M // g
for k in range(g):
    if S[k * n] != T[k * m]:
        print((-1))
        quit()
print((lcm(N, M)))

-from fractions import * +from fractions import gcd -def getDivisors(a, b): -    divisors = [] -    for i in range(2, min(a, b) + 1): -        if a % i == 0 and b % i == 0: -            divisors.append(i) -    return divisors - - -a = eval(input()) -b = eval(input()) -if a[0] != b[0]: -    print((-1)) -elif gcd(N, M) == 1: -    print((lcm(N, M))) -elif N != M: -    divisors = getDivisors(N, M) -    for i in divisors: -        k = N // i -        l = M // i -        while k < N and l < M: -            if a[k] != b[l]: -                print((-1)) -                quit() -            k += N // i -            l += M // i -    print((lcm(N, M))) -else: -    if a == b: -        print(N) -    else: +S = eval(input()) +T = eval(input()) +g = gcd(N, M) +n = N // g +m = M // g +for k in range(g): +    if S[k * n] != T[k * m]: +        quit() +print((lcm(N, M)))