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

l=n.lcm(m)

a=s.chars.map{|c|c<<" "*(l/n-1)}.join
b=t.chars.map{|c|c<<" "*(l/m-1)}.join

yes = l.times.all?{|i|
  a[i] == b[i] || a[i] == " " || b[i] == " "
}

p yes ? l : -1
