N, M = gets.split.map(&:to_i)
edges = M.times.map { gets.split.map(&:to_i) }.sort_by { |l, r, c| [l, c] }

dist = Array.new(N + 10, Float::INFINITY)
dist[1] = 0

edges.each do |l, r, c|
  dist[l + 1] = [dist[l + 1], dist[l] + c].min

  cost = dist[l] + c

  if dist[r] > cost
    (l..r).each do |i|
      dist[i] = cost if dist[i] > cost
    end
  end
end

if dist[N] == Float::INFINITY
  puts -1
else
  puts dist[N]
end
