from fractions import gcd

def lcm(x,y):

  return (x*y)//gcd(x,y)

  

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

s=eval(input())

t=eval(input())

c=0

a=lcm(n,m)//n

b=lcm(n,m)//m





for i in range(n):

  if int(i*a)%b==0:

    if s[i]==t[int(i*a/b)]:c+=1

    else:

      break

if c==(int(lcm(n,m)/lcm(a,b))):print((lcm(n,m)))

else:print((-1))
from fractions import gcd

def lcm(x,y):

  return (x*y)//gcd(x,y)



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

s=eval(input())

t=eval(input())

c=0

a=lcm(n,m)//n

b=lcm(n,m)//m



for i in range(n):

  if (i*a)%b==0:

    if s[i]==t[int(i*a/b)]:c+=1

    else:

      break

if c==(int(lcm(n,m)/lcm(a,b))):print((lcm(n,m)))

else:print((-1))
from fractions import gcd


def lcm(x, y):
    return (x * y) // gcd(x, y)


n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
c = 0
a = lcm(n, m) // n
b = lcm(n, m) // m
for i in range(n):
    if int(i * a) % b == 0:
        if s[i] == t[int(i * a / b)]:
            c += 1
        else:
            break
if c == (int(lcm(n, m) / lcm(a, b))):
    print((lcm(n, m)))
else:
    print((-1))

from fractions import gcd


def lcm(x, y):
    return (x * y) // gcd(x, y)


n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
c = 0
a = lcm(n, m) // n
b = lcm(n, m) // m
for i in range(n):
    if (i * a) % b == 0:
        if s[i] == t[int(i * a / b)]:
            c += 1
        else:
            break
if c == (int(lcm(n, m) / lcm(a, b))):
    print((lcm(n, m)))
else:
    print((-1))

-    if int(i * a) % b == 0: +    if (i * a) % b == 0: