ls, lt = list(map(int, input().split()))

s = list(eval(input()))

t = list(eval(input()))

def gcd(m,n):

    if n == 0:

        return m

    else:

        return gcd(n,m%n)

g = gcd(ls,lt)

m = ls // g

n = lt // g

ret = g*m*n

for i in range(g):

    if s[i*m] != t[i*n]:

        ret = -1

        break

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

s = eval(input())

t = eval(input())

def gcd(x, y):

    if y == 0: return x

    return gcd(y, x % y)

g = gcd(n, m)

for i in range(0, g):

    if s[i*n//g] != t[i*m//g]:

        print((-1))

        exit()

print((n*m//g))
ls, lt = list(map(int, input().split()))
s = list(eval(input()))
t = list(eval(input()))


def gcd(m, n):
    if n == 0:
        return m
    else:
        return gcd(n, m % n)


g = gcd(ls, lt)
m = ls // g
n = lt // g
ret = g * m * n
for i in range(g):
    if s[i * m] != t[i * n]:
        ret = -1
        break
print(ret)

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


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


g = gcd(n, m)
for i in range(0, g):
    if s[i * n // g] != t[i * m // g]:
        print((-1))
        exit()
print((n * m // g))

-ls, lt = list(map(int, input().split())) -s = list(eval(input())) -t = list(eval(input())) +n, m = list(map(int, input().split())) +s = eval(input()) +t = eval(input()) -def gcd(m, n): -    if n == 0: -        return m -    else: -        return gcd(n, m % n) +def gcd(x, y): +    if y == 0: +        return x +    return gcd(y, x % y) -g = gcd(ls, lt) -m = ls // g -n = lt // g -ret = g * m * n -for i in range(g): -    if s[i * m] != t[i * n]: -        ret = -1 -        break -print(ret) +g = gcd(n, m) +for i in range(0, g): +    if s[i * n // g] != t[i * m // g]: +        print((-1)) +        exit() +print((n * m // g))