from fractions import gcd



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

S = eval(input())

T = eval(input())

s = len(S)

t = len(T)



st = gcd(s, t)

good = s * t // st



if S[::s // st] == T[::t // st]:

    print(good)

else:

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

S = eval(input())

T = eval(input())

s = len(S)

t = len(T)





def gcd(a, b):

    while b:

        a, b = b, a % b

    return a





st = gcd(s, t)

good = s * t // st



if S[::s // st] == T[::t // st]:

    print(good)

else:

    print((-1))
from fractions import gcd

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
s = len(S)
t = len(T)
st = gcd(s, t)
good = s * t // st
if S[:: s // st] == T[:: t // st]:
    print(good)
else:
    print((-1))

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
s = len(S)
t = len(T)


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


st = gcd(s, t)
good = s * t // st
if S[:: s // st] == T[:: t // st]:
    print(good)
else:
    print((-1))

-from fractions import gcd - + + +def gcd(a, b): +    while b: +        a, b = b, a % b +    return a + +