class PQueue
    def initialize(n,&block)
        @buf = [nil]*n
        @cmp = block
        @size = 0
    end
    def size
        @size
    end

    def push val
#        p "push"
        @size += 1
        n = @size
        while n > 1
            nextn = n/2
            vnextn = @buf[nextn]
            break unless @cmp.call(val,vnextn)
            @buf[n] = vnextn
            n = nextn
        end
        @buf[n] = val
#        p [val,@buf[1,@size]]
    end
    def pop
#        p "pop"
#        raise "empty" if @size == 0
        if @size == 1
            ret = @buf[1]
            @size = 0
        else
            vn = @buf[@size]
            ret = @buf[1]
            @size -= 1
            n = 1
            while n*2 <= @size
                vn2,vn21 = @buf[n*2],@buf[n*2+1]
                if n*2+1 > @size || @cmp.call(vn2,vn21)
                    upper = n*2
                    vupper = vn2
                else
                    upper = n*2+1
                    vupper = vn21
                end
                break unless @cmp.call(vupper,vn)
                @buf[n] = vupper
                n = upper
            end
            @buf[n] = vn
        end
#        p [ret,@buf[1,@size]]
        ret
    end
end


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 = PQueue.new(n+m){|(a1,_),(b1,_)|a1 < b1 }
path_list.push([0,1])
costs = [1e20]*(n+1)
ans = nil
while path_list.size > 0
    cost,l = path_list.pop
    next if costs[l] < cost
    if n == l
        ans = costs[n]
        break
    end
    node[l].each{|r,c|
        if costs[r] > cost+c
            costs[r] = cost+c
            path_list.push([cost+c,r])
        end
    }
end
p ans||-1
