N, M = gets.split.map(&:to_i)
lrcs = M.times.map { gets.split.map(&:to_i) }
imos = Array.new(N, 0)
graph = Array.new(N) { [] }
lrcs.each do |(l, r, c)|
  l -= 1
  r -= 1
  graph[l].push([r, c])
  imos[l] += 1
  imos[r] -= 1 if r < N
end
current = 0
used = Array.new(N)
imos.each.with_index do |n, i|
  current += n
  used[i] = current > 0
end

used.each.with_index do |b, i|
  graph[i + 1].push([i, 0]) if b
end

MAX = 10 ** 9 * N + 1

def shortest_path(graph, s)
  c_list = Array.new(graph.size, MAX)
  c_list[s] = 0
  queue = [[s, 0]]
  while !queue.empty?
    v, c = queue.shift
    next if c > c_list[v]
    graph[v].each do |(u, c)|
      if c_list[u] > c_list[v] + c
        c_list[u] = c_list[v] + c
        queue.push([u, c_list[u]])
      end
    end
  end
  c_list
end

ans = shortest_path(graph, 0)[N - 1]
if ans == MAX
  puts '-1'
else
  puts ans
end
