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

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_str = s.split('')
t_str = t.split('')

flag = true
n.times do |s_i|
  m.times do |t_i|
    if s_arr[s_i] == t_arr[t_i]
      flag = false if s_str[s_i] != t_str[t_i]
    end
  end
end

if flag
  puts l
else
  puts '-1'
end
