import fractions 

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

gcd = int(fractions.gcd(n,m))

lcm = n * m // gcd

s = eval(input())

t = eval(input())



if gcd == 1:

  if s[0] == t[0]:

    print(lcm)

  else:

    print((-1))

else:

  if s[::n//gcd] == t[::m//gcd]:

    print(lcm)

  else:

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



def gcd(a, b):

  a, b = (a, b) if a >= b else (b, a)

  while b > 0:

    a, b = b, a % b

  return a



gc = gcd(n,m)

lcm = n * m // gc

s = eval(input())

t = eval(input())



if s[::n//gc] == t[::m//gc]:

  print(lcm)

else:

  print((-1))
import fractions

n, m = list(map(int, input().split()))
gcd = int(fractions.gcd(n, m))
lcm = n * m // gcd
s = eval(input())
t = eval(input())
if gcd == 1:
    if s[0] == t[0]:
        print(lcm)
    else:
        print((-1))
else:
    if s[:: n // gcd] == t[:: m // gcd]:
        print(lcm)
    else:
        print((-1))

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


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


gc = gcd(n, m)
lcm = n * m // gc
s = eval(input())
t = eval(input())
if s[:: n // gc] == t[:: m // gc]:
    print(lcm)
else:
    print((-1))

-import fractions +n, m = list(map(int, input().split())) -n, m = list(map(int, input().split())) -gcd = int(fractions.gcd(n, m)) -lcm = n * m // gcd + +def gcd(a, b): +    a, b = (a, b) if a >= b else (b, a) +    while b > 0: +        a, b = b, a % b +    return a + + +gc = gcd(n, m) +lcm = n * m // gc -if gcd == 1: -    if s[0] == t[0]: -        print(lcm) -    else: -        print((-1)) +if s[:: n // gc] == t[:: m // gc]: +    print(lcm) -    if s[:: n // gcd] == t[:: m // gcd]: -        print(lcm) -    else: -        print((-1)) +    print((-1))