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())

lcm=lcm(n,m)

S=[(i,int(i*(lcm/n)+1)) for i in range(n)]

L=[(i,int(i*(lcm/m)+1)) for i in range(m)]

left,right=0,0

ans=[]

while left!=n and right!=m:

    if S[left][1]==L[right][1]:

        ans.append([S[left][0],L[right][0]])

        right +=1

        left +=1

    elif S[left][1]>L[right][1]:

        right +=1

    elif S[left][1]<L[right][1]:

        left +=1

for l,r in ans:

    if s[l]==t[r]:continue

    else:print((-1));exit()

print(lcm)
from fractions import gcd

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

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

s,t=eval(input()),eval(input())

lcm=lcm(n,m)

if s[::int(lcm/m)]!=t[::int(lcm/n)]:print((-1));exit()

print(lcm)
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())
lcm = lcm(n, m)
S = [(i, int(i * (lcm / n) + 1)) for i in range(n)]
L = [(i, int(i * (lcm / m) + 1)) for i in range(m)]
left, right = 0, 0
ans = []
while left != n and right != m:
    if S[left][1] == L[right][1]:
        ans.append([S[left][0], L[right][0]])
        right += 1
        left += 1
    elif S[left][1] > L[right][1]:
        right += 1
    elif S[left][1] < L[right][1]:
        left += 1
for l, r in ans:
    if s[l] == t[r]:
        continue
    else:
        print((-1))
        exit()
print(lcm)

from fractions import gcd


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


n, m = list(map(int, input().split()))
s, t = eval(input()), eval(input())
lcm = lcm(n, m)
if s[:: int(lcm / m)] != t[:: int(lcm / n)]:
    print((-1))
    exit()
print(lcm)

-s = eval(input()) -t = eval(input()) +s, t = eval(input()), eval(input()) -S = [(i, int(i * (lcm / n) + 1)) for i in range(n)] -L = [(i, int(i * (lcm / m) + 1)) for i in range(m)] -left, right = 0, 0 -ans = [] -while left != n and right != m: -    if S[left][1] == L[right][1]: -        ans.append([S[left][0], L[right][0]]) -        right += 1 -        left += 1 -    elif S[left][1] > L[right][1]: -        right += 1 -    elif S[left][1] < L[right][1]: -        left += 1 -for l, r in ans: -    if s[l] == t[r]: -        continue -    else: -        print((-1)) -        exit() +if s[:: int(lcm / m)] != t[:: int(lcm / n)]: +    print((-1)) +    exit()