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

a = n.lcm(m)

#p a
ss = ""
s.chars{|c|
  ss += c
  (a/n-1).times{ss+=" "}
}

tt = ""
t.chars{|c|
  tt += c
  (a/m-1).times{tt+=" "}
}


ans = true
a.times{|i|
  if !(ss[i]==tt[i] || ss[i]==" " || tt[i]==" ")
    ans = false
  end
}

if ans
  p a
else
  p -1
end

