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

S=eval(input())

T=eval(input())



def gcd(a,b):

  if a<b:

    a,b=b,a

  while a%b>0:

    a,b=b,a%b

  return b



def lcm(a,b):

  return (a//gcd(a,b))*b



length=lcm(N,M)

gapt=length//N

gaps=length//M

i=0

j=0

while i<N:

  if S[i]!=T[j]:

    print((-1))

    break

  else:

    i+=gaps

    j+=gapt

else:

  print(length)
def gcd(a,b):

  if a<b:

    a,b=b,a

  while a%b>0:

    a,b=b,a%b

  return b



def lcm(a,b):

  return (a//gcd(a,b))*b



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

S=eval(input())

T=eval(input())



length=lcm(N,M)

S=S[::length//M]

T=T[::length//N]

if S==T:

  print(length)

else:

  print((-1))

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


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


def lcm(a, b):
    return (a // gcd(a, b)) * b


length = lcm(N, M)
gapt = length // N
gaps = length // M
i = 0
j = 0
while i < N:
    if S[i] != T[j]:
        print((-1))
        break
    else:
        i += gaps
        j += gapt
else:
    print(length)

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


def lcm(a, b):
    return (a // gcd(a, b)) * b


N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
length = lcm(N, M)
S = S[:: length // M]
T = T[:: length // N]
if S == T:
    print(length)
else:
    print((-1))

-N, M = list(map(int, input().split())) -S = eval(input()) -T = eval(input()) - - +N, M = list(map(int, input().split())) +S = eval(input()) +T = eval(input()) -gapt = length // N -gaps = length // M -i = 0 -j = 0 -while i < N: -    if S[i] != T[j]: -        print((-1)) -        break -    else: -        i += gaps -        j += gapt +S = S[:: length // M] +T = T[:: length // N] +if S == T: +    print(length) -    print(length) +    print((-1))