def getdup(a, b)
  res = {}
  a.each do |key, val|
    b.each do |k1, v1|
      res[key] = val if val == v1
    end
  end
  res
end

N, M = gets.chomp.split().map(&:to_i)
S = gets.chomp
T = gets.chomp
n = N.lcm(M)
i = 1
loop do
  l = n*i
  if l > 100000 || S[0] != T[0] || (N-1)*(l/N)+1 > l || (M-1)*(l/M)+1 > l
    puts -1
    break
  else
    valN = {}
    p = 0
    loop do
      break if p > (N-1)
      if p == 0
        valN[p] = 1
      else
        valN[p] = p*(l/N)+1
      end
      p += 1
    end
    valM = {}
    p = 0
    loop do
      break if p > (M-1)
      if p == 0
        valM[p] = 1
      else
        valM[p] = p*(l/M)+1
      end
      p += 1
    end
    a = getdup(valN, valM)
    b = getdup(valM, valN)
    status = false
    a.keys.length.times do |s|
      if S[a.keys[s]] != T[b.keys[s]]
        status = true
      end
    end
    unless status
      puts l
      break
    end
  end
  i += 1
end