n, m = gets.split.map(&:to_i)
s = gets.chomp
t = gets.chomp
size = n.lcm(m)
x = Array.new(size)

s.each_char.with_index { |c, idx| x[size / n * idx] = c }

t.each_char.with_index do |c, idx|
  i = size / m * idx

  if x[i] && x[i] != c
    puts -1
    exit
  end
end

puts size