require "prime"
class Array
  def lcm
    self.inject(:lcm)
  end
end
n,m=gets.split.map(&:to_i)
s=gets.chomp
t=gets.chomp
lcm=[n,m].lcm
sdis=lcm/n
tdis=lcm/m
min= n<=m ? n:m
dis= n<=m ? tdis:sdis
1.step(min-dis,dis) do |i|
  if s[tdis]!=t[sdis]
    puts -1
    exit
  end
  sdis*=2
  tdis*=2
end
puts lcm
