function gcd(u, v)
  while v ~= 0 do
    u, v = v, u%v
  end
  return u
end

function lcm(u,v)
  return u * v / gcd(u, v)
end

N, M = string.match(io.read(), "(%d) (%d)")
S = io.read()
T = io.read()

L = lcm(N,M)
ans = L

oS = L/N
oT = L/M

v = 1
X = {}
for c in string.gmatch(S, "%a") do
  X[v] = c
  v = v + oS
end

v = 1
for c in string.gmatch(T, "%a") do
  if not (X[v] == nil or X[v] == c) then
    ans = -1
    break
  end
  v = v + oT
end

print(string.format("%d",ans))
