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

s = eval(input())

t = eval(input())

import math



# 良い文字列が作れるなら、答えはLCM(n, m)となる

lcm = n * m // math.gcd(n, m)



x = {}

for i,c in enumerate(s):

    # (pos, char)

    x[lcm//n*i] = c



for i,c in enumerate(t):

    # (pos, char)

    pos, char = lcm//m*i, c

    if pos in x and x[pos] != char:

        print((-1))

        exit()



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

s = eval(input())

t = eval(input())

import math



# 良い文字列が作れるなら、答えはLCM(n, m)となる

gcd = math.gcd(n, m)

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

    print((n * m // gcd))

else:

    print((-1))
n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
import math

# 良い文字列が作れるなら、答えはLCM(n, m)となる
lcm = n * m // math.gcd(n, m)
x = {}
for i, c in enumerate(s):
    # (pos, char)
    x[lcm // n * i] = c
for i, c in enumerate(t):
    # (pos, char)
    pos, char = lcm // m * i, c
    if pos in x and x[pos] != char:
        print((-1))
        exit()
print(lcm)

n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
import math

# 良い文字列が作れるなら、答えはLCM(n, m)となる
gcd = math.gcd(n, m)
if s[:: n // gcd] == t[:: m // gcd]:
    print((n * m // gcd))
else:
    print((-1))

-lcm = n * m // math.gcd(n, m) -x = {} -for i, c in enumerate(s): -    # (pos, char) -    x[lcm // n * i] = c -for i, c in enumerate(t): -    # (pos, char) -    pos, char = lcm // m * i, c -    if pos in x and x[pos] != char: -        print((-1)) -        exit() -print(lcm) +gcd = math.gcd(n, m) +if s[:: n // gcd] == t[:: m // gcd]: +    print((n * m // gcd)) +else: +    print((-1))