from fractions import gcd

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

s = eval(input())

t = eval(input())

x = gcd(n,m)

for i in range(x):

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

    print((-1))

    break

else:

  print((n*m//x))
from fractions import gcd

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

s = eval(input())

t = eval(input())

x = gcd(n,m)

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

  print((n*m//x))

else:

  print((-1))
from fractions import gcd

n, m = list(map(int, input().split()))
s = eval(input())
t = eval(input())
x = gcd(n, m)
for i in range(x):
    if s[i * n // x] != t[i * m // x]:
        print((-1))
        break
else:
    print((n * m // x))

from fractions import gcd

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

-for i in range(x): -    if s[i * n // x] != t[i * m // x]: -        print((-1)) -        break +if s[:: n // x] == t[:: m // x]: +    print((n * m // x)) -    print((n * m // x)) +    print((-1))