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

if s[0] == t[0]

  l = n.lcm(m)

  s_diff = l / n
  t_diff = l / m

  s_arr = []
  t_arr = []

  n.times do |i|
    s_arr << i * s_diff + 1
  end

  m.times do |i|
    t_arr << i * t_diff + 1
  end

  s_is = [0]
  t_is = [0]
  i = 1
  loop do
    next_num = i * t_diff
    if next_num >= n
      break
    else
      s_is << next_num
    end
    i += 1
    # s_is << i * t_diff
    # i += 1
    # if s_is.last >= n
    #   s_is.pop
    #   break
    # end
  end
  i = 1
  loop do
    next_num = i * s_diff
    if next_num >= m
      break
    else
      t_is << next_num
    end
    i += 1
    # t_is << i * s_diff
    # i += 1
    # if t_is.last >= m
    #   t_is.pop
    #   break
    # end
  end

  # p s_is, t_is

  flag = true
  s_is.each do |s_i|
    break unless flag
    t_is.each do |t_i|
      next unless s_arr[s_i] == t_arr[t_i]
      if s[s_i] != t[t_i]
        flag = false
        break
      end
    end
  end

  if flag
    puts l
  else
    puts '-1'
  end

else
  puts '-1'
end
