n,m = gets.split.map(&:to_i)
node = [[],[]]+(2..n).map{|i|[[i-1,0]]}

m.times{
    l,r,c=gets.split.map(&:to_i)
    node[l] << [r,c]
}

path_list = [[0,1]]
gone = [false]*(n+1)
while path_list[0]
    cost,l = path_list.shift
    next if gone[l]
    gone[l] = true
    if l == n
        p cost
        exit
    end
    node[l].each{|r,c|
        unless gone[r]
          	tmpcost = cost+c
            path_list.insert((0..path_list.size-1).bsearch{|i|path_list[i][0]>tmpcost}||path_list.size,[tmpcost,r])
        end
    }
end
p -1
