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

def lcm(a,b)
  a*b/gcd(a,b)
end

n,m = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp
t = gets.chomp
nm = gcd(n,m)
i = n/nm
j = m/nm
(nm+1).times do |k|
  if s[k*i] != t[k*j]
    puts "-1"
    exit
  end
end
puts lcm(n,m)