N,M=gets.chomp.split(" ").map(&:to_i)
edge_list = []
M.times do
    l,r,c=gets.chomp.split(" ").map(&:to_i)
    (l..(r-1)).each do |ll|
        ((ll+1)..r).each do |rr|
            edge_list.push([ll, rr, c])
            edge_list.push([rr, ll, c])
        end
    end
end

INF = 10**10
def bellman_ford(edge_list, s, max_count)
    d = Array.new(N+1, INF)
    d[s] = 0
    c = 0
    loop do
      is_update = false
      edge_list.each do |(u, v, c)|
        if d[u] != INF && d[u] + c < d[v]
          d[v] = d[u] + c
          is_update = true
        end
      end
      return d if !is_update
      c += 1
      return nil if c == max_count
    end
end

ret = bellman_ford(edge_list, 1, N)

if ret == nil || ret[N] == INF
    puts "-1"
else
    puts ret[N]
end