You are given two strings, S and T, both consisting of lowercase English letters. A string X is considered a "good string" if its length is divisible by both the lengths of S and T, and if specific concatenations of its characters result in S and T. Your task is to determine if a good string exists and, if so, find the length of the shortest such string. The input is given in the format N M S T, and the output should be the length of the shortest good string or -1 if it does not exist.

# Buggy version:
from collections import defaultdict
import math
n,m = map(int, input().split())
s = input()
t = input()

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

# Fixed version:
import math
n,m = map(int, input().split())
s = input()
t = input()

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

# Buggy version:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]

for j, i in enumerate(b0):
    if i in set(b1):
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
    
for j, i in enumerate(b1):
    if i in set(b0):
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)


# Fixed version:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]
b2 = set(b0)
b3 = set(b1)

for j, i in enumerate(b0):
    if i in b3:
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
            
for j, i in enumerate(b1):
    if i in b2:
        if s[b0.index(i)] != t[j]:
            print(-1)
            sys.exit()
print(a)

# Buggy version:
N, M = map(int, input().split())
S = input()
T = input()

from collections import gcd

def solve(N,M,S,T):
    g = gcd(N,M)
    for i in range(g):
        if S[N//g*i]!=T[M//g*i]:
            return -1
    ans = N*M//g
    return ans
print(solve(N,M,S,T))

# Fixed version:
from math import gcd
def solve():
  N, M = map(int, input().split())
  S = input()
  T = input()
  g = gcd(N,M)
  ans = N*M//g
  for i in range(g):
    if S[N//g*i]!=T[M//g*i]:
      return -1
  return ans
print(solve())

# Buggy version:
import math
def main5():
    N, M = map(int, input().split())
    S = input()
    T = input()

    G = math.gcd(N, M)
    L = (N * M) // G

    for i in range(G):
        if S[i * N // G] != T[i * M // G]:
            print(-1)
            exit()

    print(L)

if __name__ == "__main__":
    main4()

# Fixed version:
import math
def main5():
    N, M = map(int, input().split())
    S = input()
    T = input()

    G = math.gcd(N, M)
    L = (N * M) // G

    for i in range(G):
        if S[i * N // G] != T[i * M // G]:
            print(-1)
            exit()

    print(L)

if __name__ == "__main__":
    main5()

# Buggy version:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b


N, M = map(int, input().split())
S = input()
T = input()
x = gcd(N, M)
n = N // x
m = M // x

for i in range(3):
    if S[n*i] == T[m*i]:
        print(-1)
        break
print(N*M//x)


# Fixed version:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b


N, M = map(int, input().split())
S = input()
T = input()
x = gcd(N, M)
n = N // x
m = M // x

for i in range(x):
    if S[n*i] != T[m*i]:
        print(-1)
        exit()
print(N*M//x)

# Buggy version:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
g = gcd(n,m)

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

print(l)


# Fixed version:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
g = math.gcd(n,m)

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

print(l)

# Buggy version:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    g = gcd(n, m)
    ans = n * m // g
    a, b = n // g, m // g
    x, y = 0, 0
    for i in range(g):
        if s[x] != x[y]:
            print(-1)
            return
        x += a
        y += b
    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""


# Fixed version:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    g = gcd(n, m)
    ans = n * m // g
    a, b = n // g, m // g
    x, y = 0, 0
    for i in range(g):
        if s[x] != t[y]:
            print(-1)
            return
        x += a
        y += b
    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""

# Buggy version:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
g=gcd(a,b)
if a%b==0 or b%a==0:
    for i in range(g):
        if s[a//g*i]!=t[b//g*i]:
            print(-1)
            exit()
if t[0]!=s[0]:
    print(-1)
    exit()
print(a*b*g)

# Fixed version:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
g=gcd(a,b)
for i in range(g):
    if s[a//g*i]!=t[b//g*i]:
        print(-1)
        exit()
print(a*b//g)

# Buggy version:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
s = input()
t = input()
l = n*m//gcd(n,m)

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

# Fixed version:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
s = input()
t = input()
k = gcd(n,m)
l = n*m//k

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

# Buggy version:
N, M=map(int, input().split())
s=input()
t=input()
from math import gcd

g=gcd(N,M)

if g==1:
  ans=(N*M)//g
else:
  n=N//g
  m=M//g
  import numpy as np
  pn=np.arange(m, N, m)
  qm=np.arange(n, M, n)
  ans=(N*M)//g
  for i in range(len(pn)):
    if s[pn[i]]!=t[qm[i]]:
      ans=-1
      break
print(ans)
  
  
  

# Fixed version:
N, M=map(int, input().split())
s=input()
t=input()
from math import gcd

g=gcd(N,M)

if g==1:
  ans=(N*M)//g
else:
  n=N//g
  m=M//g
  import numpy as np
  pn=np.arange(m, M, m)
  qm=np.arange(n, N, n)
  ans=(N*M)//g
  for i in range(len(pn)):
    if s[qm[i]]!=t[pn[i]]:
      ans=-1
      break
if s[0]!=t[0]:
  ans=-1
print(ans)

# Buggy version:
from math import gcd
N,M = map(int,input().split())
s = input()
t = input()
g = gcd(N,M)
l = N*M//g

n = N//g
m = M//g

ans = l

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



# Fixed version:
import math

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

gcd = math.gcd(n, m)
N = n // gcd
M = m // gcd
ans = n * m // gcd
for i in range(gcd):
    if s[i*N] != t[i*M] :
        ans = -1
        break
print(ans)

# Buggy version:
N, M = map(int, input().split())
S = input()
T = input()

import sys

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

g = gcd(N, M)
sn = N/g
sm = M/g

ok = True
for k in range(0, g):
    a = k * sn
    b = k * sm
    if S[a] != T[b]:
        ok = False
        break

if ok:
    print(N*M/g)
else:
    print(-1)

# Fixed version:
N, M = map(int, input().split())
S = input()
T = input()

import sys

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


g = gcd(N, M)
sn = N//g
sm = M//g

ok = True
for k in range(0, g):
    a = k * sn
    b = k * sm
    if S[a] != T[b]:
        ok = False
        break

if ok:
    print(N*M//g)
else:
    print(-1)

# Buggy version:
n , m = map(int, input().split())
s = input()
t = input()

def gcd(num1,num2):
    r=1
    while r!=0:
        r=num1%num2
        num1=num2
        num2=r
    return num1

def lcm(num1,num2):
    return num1*num2//gcd(num1,num2)

g1 = lcm(n,m)
g2 = lcm(g1//n,g1//m)

s*=g1//n
t*=g1//m

for i in range(g1//g2):
    if s[g2*i]!=t[g2*i]:
        print('-1')
        exit()
print(g1)


# Fixed version:
n , m = map(int, input().split())
s = input()
t = input()

def gcd(num1,num2):
    r=1
    while r!=0:
        r=num1%num2
        num1=num2
        num2=r
    return num1

def lcm(num1,num2):
    return num1*num2//gcd(num1,num2)

g1 = lcm(n,m)
g2 = lcm(g1//n,g1//m)

for i in range(g1//g2):
    if s[g2*n*i//g1]!=t[g2*m*i//g1]:
        print('-1')
        exit()
print(g1)

# Buggy version:
#import sys
n, m = map(int, input().split())
s = input()
t = input()

if s[0] != t[0]:
    print(-1)
    exit()
    #sys.exit(0)

#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)

l = lcm(n, m)
step = max(l//n, l//m)

for i in range(1, l, step):
    if i % (l//n) == 0 and i % (l//m) == 0:
        if s[i * n // l] != t[i * m // l]:
            print(-1)
            exit()
            # sys.exit(0)

print(l)


# Fixed version:
#import sys
n, m = map(int, input().split())
s = input()
t = input()

if s[0] != t[0]:
    print(-1)
    exit()
    #sys.exit(0)

#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)

l = lcm(n, m)
step = min(l//n, l//m)

for i in range(0, l, step):
    if i % (l//n) == 0 and i % (l//m) == 0:
        if s[i * n // l] != t[i * m // l]:
            print(-1)
            exit()
            # sys.exit(0)

print(l)

# Buggy version:
N,M = map(int,input().split())
def gcd(a,b):
    if a >= b:
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    else:
        if a == 0:
            return b
        else:
            return gcd(a,b%a)
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    flag = True
    L = int((N//gcd(N,M))*M)
    m = L//M
    n = L//N
    for i in range(min(N,M)):
        if i*m <= N-1 and i*n <= M-1:
            if S[i*m*L//N] != T[i*n*L//M]:
                flag = False
                break
    if flag:
        print(L)
    else:
        print(-1)


# Fixed version:
N,M = map(int,input().split())
def gcd(a,b):
    if a >= b:
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    else:
        if a == 0:
            return b
        else:
            return gcd(a,b%a)
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    flag = True
    L = int((N//gcd(N,M))*M)
    m = L//M
    n = L//N
    for i in range(max(N,M)):
        if i*m <= N-1 and i*n <= M-1:
            if S[i*m] != T[i*n]:
                flag = False
                break
    if flag:
        print(L)
    else:
        print(-1)

# Buggy version:
# 2019-11-28 02:28:10(JST)
import sys

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t = sys.stdin.read().split()

    g = gcd(n, m)
    l = lcm(n, m)

    if g == 1:
        if s[0] == s[1]:
            ans = l
    else:
        for i in range(g):
            if s[i * n // g] == t[i * m // g]:
                continue
            else:
                ans = -1
                break
        else:
            ans = l
    
    print(ans)

if __name__ == '__main__':
    main()


# Fixed version:
# 2019-11-28 02:28:10(JST)
import sys

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t = sys.stdin.read().split()

    g = gcd(n, m)
    l = lcm(n, m)

    if g == 1:
        if s[0] == t[0]:
            ans = l
        else:
            ans = -1
    else:
        for i in range(g):
            if s[i * n // g] == t[i * m // g]:
                continue
            else:
                ans = -1
                break
        else:
            ans = l
    
    print(ans)

if __name__ == '__main__':
    main()

# Buggy version:
N,M = map(int,input().split())
S = input()
T = input()

def gcd(x, y):
    while y > 0:
        x, y = y, x%y
    return x
 
def lcm(x, y):
    return int(x/gcd(x, y)*y)

lenx = lcm(N,M)

s_list = list(S)
t_list = list(T)

baisuu_s = lenx/N
baisuu_t = lenx/M
def check():
    for i in range(0,lenx,baisuu_s):
        if i % baisuu_s==0 and i%baisuu_t == 0:
            if s_list[int(i/baisuu_s)] == t_list[int(i/baisuu_t)]:
                pass
            else:
                return False
    return True

if check():
    print(lenx)
else:
    print(-1)

# Fixed version:
N,M = map(int,input().split())
S = input()
T = input()

def gcd(x, y):
    while y > 0:
        x, y = y, x%y
    return x
 
def lcm(x, y):
    return int(x/gcd(x, y)*y)

lenx = lcm(N,M)

s_list = list(S)
t_list = list(T)

baisuu_s = int(lenx/N)
baisuu_t = int(lenx/M)
def check():
    for i in range(0,lenx,baisuu_s):
        if i % baisuu_s==0 and i%baisuu_t == 0:
            if s_list[int(i/baisuu_s)] == t_list[int(i/baisuu_t)]:
                pass
            else:
                return False
    return True

if check():
    print(lenx)
else:
    print(-1)

# Buggy version:
def main28():
    import sys
    N, M = (int(x) for x in input().split())

    if N >= M:
        S = input()
        T = input()
    else:
        T = input()
        S = input()

    def lcm(n, m):
        gcd = 0
        for i in range(1, min(n, m)):
            if n % i == 0 and m % i == 0:
                gcd = i

        return int(n * m / gcd)

    LCM = lcm(N, M)
    a = int(LCM / max(N, M))
    b = int(LCM / min(N, M))

    for i in range(b):
        small_index = i * b
        if small_index % a == 0 and S[int(small_index / a)] != T[i]:
            print("-1")
            sys.exit()
    print(LCM)

main28()

# Fixed version:
def main28():
    import sys
    N, M = (int(x) for x in input().split())

    if N >= M:
        S = input()
        T = input()
    else:
        T = input()
        S = input()

    def lcm(n, m):
        gcd = 0
        for i in range(1, min(n, m) + 1):
            if n % i == 0 and m % i == 0:
                gcd = i

        return int(n * m / gcd)

    LCM = lcm(N, M)

    a = int(LCM / max(N, M))
    b = int(LCM / min(N, M))

    for i in range(min(N, M)):
        small_index = i * b
        if int(small_index % a) == 0:
            if S[int(small_index / a)] != T[i]:
                print("-1")
                sys.exit()
    print(LCM)

main28()

# Buggy version:
n,m=map(int, input().split())
s=input()
t=input()
def euc(x,y):
  if x % y==0:
    return x
  else:
    return euc(max(x,y) % min (x,y),min(x,y))
d=euc(n,m)
ans=n*m
if d==1:
  print(n*m)
else:
  for i in range(d):
    if s[i*int(n/d)]!=t[i*int(m/d)]:
      ans=-1
  print(ans)

# Fixed version:
n,m=map(int, input().split())
s=input()
t=input()
def euc(x,y):
  if max(x,y) % min(x,y)==0:
    return min(x,y)
  else:
    return euc(max(x,y) % min (x,y),min(x,y))
d=euc(n,m)
ans=n*int(m/d)
if d==1:
  if s[0]==t[0]:
    print(n*m)
  else:
    print(-1)
else:
  for i in range(d):
    if s[i*int(n/d)]!=t[i*int(m/d)]:
      ans=-1
  print(ans)

# Buggy version:
n,m=map(int,input().split())
s=input()
t=input()

def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y
#最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

L=lcm(n,m)
g=gcd(n,m)
if s[0]==t[0]:
  X=[""]*(L+1)
  print(X)
else:
  print(-1)
  exit()
for i in range(1,g):
  X[int(i*L/n)+1]=s[i]
for i in range(1,g):
  if X[int(i*L/m)+1] !="":
    if X[int(i*L/m)+1] !=t[i]:
      print(-1)
      exit()
print(L)

# Fixed version:
n,m=map(int,input().split())
s=input()
t=input()

def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y
#最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

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

# Buggy version:
N,M = map(int,input().split())
S = input()
T = input()
ans = 0
a = 2

if S[0] != T[0]:ans = -1

elif N % M != 0 and M % N != 0:
    if N >= M:
        while True:
            if N * a % M == 0:
                ans = N * a
                b = int(N*a/M)
                for i in range(M):
                    if T[i] != S[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1
    elif N < M:
        while True:
            if M * a % N == 0:
                ans = M * a
                b = int(M*a/N)
                for i in range(N):
                    if S[i] != T[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1

elif N % M == 0:
    b = int(N/M)
    ans = N
    for i in range(M):
        if T[i] != S[b*i]:
            ans = -1
            break   

elif M % N == 0:
    b = int(M/N)
    ans = M
    for i in range(N):
        if S[i] != T[b*i]:
            ans = -1
            break   

print(ans)


# Fixed version:
N,M = map(int,input().split())
S = input()
T = input()
ans = 0
a = 2

if S[0] != T[0]:ans = -1

elif N % M != 0 and M % N != 0:
    if N >= M:
        while True:
            if N * a % M == 0:
                ans = N * a
                b = int(N*a/M)
                for i in range(int(N/b)):
                    if T[a*i] != S[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1
    elif N < M:
        while True:
            if M * a % N == 0:
                ans = M * a
                b = int(M*a/N)
                for i in range(int(M/b)):
                    if T[b*i] != S[a*i]:
                        ans = -1
                        break
                break
            else:
                a += 1

elif N % M == 0:
    b = int(N/M)
    ans = N
    for i in range(M):
        if T[i] != S[b*i]:
            ans = -1
            break   

elif M % N == 0:
    b = int(M/N)
    ans = M
    for i in range(N):
        if S[i] != T[b*i]:
            ans = -1
            break   

print(ans)

# Buggy version:
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

x,y=map(int,input().split())
S=input()
T=input()
GCD=gcd(x,y)
LCM=(x * y) // GCD

x_new=x//GCD
y_new=y//GCD
for i in range(0,GCD-1):
  if S[(x_new)*i]==T[(y_new)*i]:
    result=LCM
  else:
    result=-1
    break
print(result)

# Fixed version:
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

x,y=map(int,input().split())
S=input()
T=input()
GCD=gcd(x,y)
LCM=(x * y) // GCD

x_new=x//GCD
y_new=y//GCD
for i in range(0,GCD):
  if S[(x_new)*i]==T[(y_new)*i]:
    result=LCM
  else:
    result=-1
    break
print(result)

# Buggy version:
N,M=map(int,input().split())
S=input()
T=input()
list_S=list(S)
list_T=list(T)
Number_i=[i for i in range(M-1)]
Number_j=[j for j in range(N-1)]
Kaburi_i=[]
Kaburi_j=[]
counter=0
Flag=0
for i in Number_i:
  if i*N/M in Number_j:
    Kaburi_i.append(i)
    Kaburi_j.append(int(i*N/M))
Kaburi_Size=len(Kaburi_i)
while counter<=Kaburi_Size-1:
  if list_S[Kaburi_i[counter]]==list_T[Kaburi_j[counter]]:
    pass
  else:
    Flag=1
    break
  counter+=1
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a
if Flag==1:
  print(-1)
else:
  print(int(N*M/gcd(N,M)))
  
  




# Fixed version:
N,M=map(int,input().split())
S=input()
T=input()
list_S=list(S)
list_T=list(T)
Number_i=[i for i in range(N)]
Number_iMN=[]
for i in Number_i:
  Number_iMN.append(i*M/N)
Number_j=[j for j in range(M)]
Kaburi_j=list(set(Number_iMN)&set(Number_j))
Kaburi_i=[]
for j in Kaburi_j:
  Kaburi_i.append(int(j*N/M))
counter=0
Flag=0
Kaburi_Size=len(Kaburi_i)
while counter<=Kaburi_Size-1:
  if list_S[int(Kaburi_i[counter])]==list_T[int(Kaburi_j[counter])]:
    pass
  else:
    Flag=1
    break
  counter+=1
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a
if Flag==1:
  print(-1)
else:
  print(int(N*M/gcd(N,M)))

# Buggy version:
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def main():
    N, M = map(int, input().split())
    S, T = input().split()

    g = gcd(N, M)
    for i in range(g):
        if S[i * N / g] == T[i * M / g]:
            print("-1")
            exit()
    print(N * M / g)

main()


# Fixed version:
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def main():
    N, M = map(int, input().split())
    S = input()
    T = input()

    g = gcd(N, M)
    for i in range(g):
        if S[i * N // g] != T[i * M // g]:
            print("-1")
            exit()
    print(N * M // g)

main()

# Buggy version:
def gcd(a, b):
    if b > a:
        gcd(b, a)
    while b:
	    a, b = b, a % b
    return a

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

l = n * m // gcd(n, m)
a = l // n
b = l // m
#print(a, b)
#a(i-1) = b(j-i) -> ai = bj + (a-b)
res = False
clear_all = True
for i in range(1,n+1):
    if (a*i - (a-b)) % b != 0:
        j = a * i - (a-b)
        if not s[i-1] == t[j-1]:
            clear_all = False
if clear_all:
    res = True
if res:
    print(l)
else:
    print(-1)
        

# Fixed version:
def gcd(a, b):
    if b > a:
        gcd(b, a)
    while b:
	    a, b = b, a % b
    return a

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

l = n * m // gcd(n, m)
a = l // n
b = l // m
#print(a, b)
#a(i-1) = b(j-i) -> ai = bj + (a-b)
res = False
clear_all = True
for i in range(1,n+1):
    if (a*i - (a-b)) % b == 0:
        j = (a * i - (a-b)) // b
        if not s[i-1] == t[j-1]:
            clear_all = False
if clear_all:
    res = True
if res:
    print(l)
else:
    print(-1)

# Buggy version:
import sys
N,M = [int(n) for n in input().split()]
S = str(input())
T = str(input())

# N < M
def gcd(N,M):
    if(N > M):
        tmp = N
        N = M
        M = tmp
    r = M % N
    while(r != 0):
        M = N
        N = r
        r = M%N
    return N

def lcm(N,M):
    return(N*M/gcd(N,M))

if (N % M == 0):
    if S == T:
        print(M)
    else:
        p = int(N/M)
        for i in range(M):
            print(T[i],S[i*p])
            if T[i] != S[i*p]:
                print(-1)
                sys.exit()
        print(N)
elif (M % N == 0):
    if S == T:
        print(M)
    else:
        p = int(M/N)
        for i in range(N):
            print(S[i],T[i*p])
            if S[i] != T[i*p]:
                print(-1)
                sys.exit()
        print(M)
else:
    print(int(lcm(N,M)))


# Fixed version:
import sys
N,M = [int(n) for n in input().split()]
S = str(input())
T = str(input())

# N < M
def gcd(N,M):
    if(N > M):
        tmp = N
        N = M
        M = tmp
    r = M % N
    while(r != 0):
        M = N
        N = r
        r = M%N
    return N

def lcm(N,M):
    return(N*M/gcd(N,M))

if (N % M == 0):
    if S == T:
        print(M)
    else:
        p = int(N/M)
        for i in range(M):
            if T[i] != S[i*p]:
                print(-1)
                sys.exit()
        print(N)
elif (M % N == 0):
    if S == T:
        print(M)
    else:
        p = int(M/N)
        for i in range(N):
            if S[i] != T[i*p]:
                print(-1)
                sys.exit()
        print(M)
else:
    mngcd = gcd(N,M)
    k = int(N/mngcd)
    m = int(M/mngcd)
    for i in range(mngcd):
        if S[i*k] != T[i*m]:
            print(-1)
            sys.exit()
    print(int(lcm(N,M)))

# Buggy version:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
    
N,M = map(int,Input().split())
s = input()
t = input()
g = gcd(N,M)
l = N*M//g

n = N//g
m = M//g

ans = l

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

# Fixed version:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
    
N,M = map(int,input().split())
s = input()
t = input()
g = gcd(N,M)
l = N*M//g

n = N//g
m = M//g

ans = l

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

# Buggy version:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

N, M = map(int, input().split())
S = input()
T = input()

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = gcd(N,M)

for i in range(0,lcm_num,gcd_num):
    if i == 0 or (i%(lN) == 0 and i%(lM) == 0):
        if S[i//lN] == T[i//lM]:
            continue
        else:
            print(-1)
            exit()

print(lcm_num)


# Fixed version:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

N, M = map(int, input().split())
S = input()
T = input()

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)

# Buggy version:
def gcd(a,b):
  if a%b == 0:
    return b
  return gcd(b,a//b)


N,M = [int(i) for i in input().split()]

S = input()
T = input()

x = N * M // gcd(N,M)
flag = True

#print(S[0 * (N / math.gcd(N,M)) + 1])
for i in range(gcd(N,M)):
  if not(S[i * (N //gcd(N,M))] == T[i * (M // gcd(N,M))]):
    flag = False
#    print(S[i * (N // math.gcd(N,M))]  , T[i * (M // math.gcd(N,M))])
if flag:
  print(x)
else:
  print('-1')

# Fixed version:
def gcd(a,b):
  if a%b == 0:
    return b
  return gcd(b,a%b)


N,M = [int(i) for i in input().split()]

S = input()
T = input()

x = N * M // gcd(N,M)
flag = True

#print(S[0 * (N / math.gcd(N,M)) + 1])
for i in range(gcd(N,M)):
  if not(S[i * (N //gcd(N,M))] == T[i * (M // gcd(N,M))]):
    flag = False
#    print(S[i * (N // math.gcd(N,M))]  , T[i * (M // math.gcd(N,M))])
if flag:
  print(x)
else:
  print('-1')

# Buggy version:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
for i in range(N) :
    A.append(i*L//N+1)
for j in range(M) :
    b = j*L//M+1
    if j % M == 0 :
        index = N*j//M
        if S[index] == T[j] :
            counter += 1
        else :
            flag = 1
    else :
        counter += 1
if flag == 1 :
    print(-1)
else :
    if counter == M :
        print(L)



# Fixed version:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
if N >= M :
    for i in range(N) :
        A.append(i*L//N+1)
    for j in range(M) :
        b = j*L//M+1
        if j*N % M == 0 :
            index = N*j//M
            if S[index] == T[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == M :
            print(L)
else :
    for i in range(M) :
        A.append(i*L//M+1)
    for j in range(N) :
        b = j*L//N+1
        if j*M % N == 0 :
            index = M*j//N
            if T[index] == S[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == N :
            print(L)

# Buggy version:
def gcd(A, B):
    while B != 0:
        A, B = B, A%B
    return A

N, M = map(int, input().split())
S = input()
T = input()
agc = N * M // gcd(N, M)
can = True
i = 0
while i*L_M < agc and i*L_N < agc:
    if S[i*L_M] != T[i*L_N]:
        can = False
        break
    i += 1
    if can:
    print(agc)
else:
    print(-1)

# Fixed version:
def gcb(A, B):
    while B != 0:
        A, B = B, A%B
    return A

N, M = map(int, input().split())
S = input()
T = input()
can = True
agc = N // gcb(N, M) * M
L_N = agc // N
L_M = agc // M
i = 0
while i*L_M < N and i*L_N < M:
    if S[i*L_M] != T[i*L_N]:
        can = False
        break
    i += 1

if can:
    print(agc)
else:
    print(-1)

# Buggy version:
N,M = map(int,input().split())
S = input()
T = input()

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

def lcm(a,b):
    return a*b//gcd(a,b)

L = lcm(N,M)
G = gcd(N,M)
n = L//N
m = L//M

s = [S[i*m] for i in range(G)]
t = [T[i*n] for i in range(G)]

if s == t or (G == 1 and S[0]==T[0}]):
    print(L)
else:
    print(-1)


# Fixed version:
N,M = map(int,input().split())
S = input()
T = input()

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

def lcm(a,b):
    return a*b//gcd(a,b)

L = lcm(N,M)
G = gcd(N,M)
n = L//N
m = L//M

s = [S[i*m] for i in range(G)]
t = [T[i*n] for i in range(G)]

if s == t or (G == 1 and S[0]==T[0]):
    print(L)
else:
    print(-1)

# Buggy version:
N, M = map(int, input().split())
S = input()
T = input()

def _gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

gcd = _gcd(N,M)
lcm = N*M//gcd

def check():
  a,b = 0,0
  da,db = N//gcd, M//gcd
  while a < N:
    if S[a]!=T[b]:
      print(a, S[a])
      print(b, T[b])
      print(-1)
      return
    a+=da
    b+=db
  print(lcm)

check()  


# Fixed version:
N, M = map(int, input().split())
S = input()
T = input()

def _gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

gcd = _gcd(N,M)
lcm = N*M//gcd

def check():
  a,b = 0,0
  da,db = N//gcd, M//gcd
  while a < N:
    if S[a]!=T[b]:
      print(-1)
      return
    a+=da
    b+=db
  print(lcm)

check()

# Buggy version:
import numpy as np
import scipy
def gcd(a, b):
    if a % b:
        return gcd(b, (a % b))
    else:
        return b
N, M = map(int,input().split())
S = input()
T = input()
t = gcd(N,M)
n = N // t
m = M // t
ans = n * m * t
for i in range(t):
    if(S[n*m*i]!=T[n*m*i]):
        ans = -1
        break
print(ans)



# Fixed version:
import numpy as np
import scipy
def gcd(a, b):
    if a % b:
        return gcd(b, (a % b))
    else:
        return b
N, M = map(int,input().split())
S = input()
T = input()
t = gcd(N,M)
n = N // t
m = M // t
ans = n * m * t
for i in range(t):
    if(S[n*i]!=T[m*i]):
        ans = -1
        break
print(ans)