m, n = gets.split.map(&:to_i)
s = gets.chomp
t = gets.chomp

def gcd(a, b)
  gcd(b, a) if b > a
  return b if (r = a % b).zero?
  gcd(b, r)
end

def solve(m, n, s, t)
  gcd(m, n).times do |i|
    if s[m/(i+1)] != t[n/(i+1)]
      return -1
    end
  end
  return m * n / gcd(m, n)
end

puts solve(m, n, s, t)
