local mfl = math.floor
local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end

local n, m = io.read("*n", "*n", "*l")
local s = io.read()
local t = io.read()
local gcd = getgcd(n, m)
local lcm = mfl(n / gcd) * m
local step_s = mfl(m / gcd)
local step_t = mfl(n / gcd)
local tbl = {}
local sstr = ""
for i = 1, n do
  local pos = 1 + (i - 1) * step_s
  sstr = s:sub(i, i)
  tbl[pos] = sstr
end
local f = true
for i = 1, m do
  local pos = 1 + (i - 1) * step_t
  if tbl[pos] ~= nil and tbl[pos] ~= t:sub(i, i) then
    f = false
    break
  end
end
print(f and lcm or -1)
