
n, m = gets.chomp.split(" ").map(&:to_i);

s = gets.strip()
t = gets.strip()

l = n * m /  m.gcd(n)


lll = [nil] * l

for i in 0...n do
  lll[1+ i* l / n] = s[i]
end


result = true

for i in 0...m do
  if lll[1+ i* l / m] == nil
      lll[1+i* l / m] = t[i]
  else
      if lll[1+i* l / m] != t[i]
          result = false
          break
        end
      end
end


if result
    print(l)
else
    print(-1)
  end
