class PQueue
    def initialize(&block)
        @buf = [nil]
        @cmp = block
    end
    def size
        @buf.size-1
    end

    def push val
#        p "push"
        @buf << val
        n = size
        while n > 1
            nextn = n/2
            vn,vnextn = @buf[n],@buf[nextn]
            if @cmp.call(vn,vnextn)
                @buf[nextn],@buf[n] = vn,vnextn
                n = nextn
            else
                break
            end
        end
#        p val
    end
    def pop
#        p "pop"
        raise "empty" if size == 0
        return @buf.pop if size == 1
        ret = @buf[1]
        @buf[1] = @buf.pop
        n = 1
        tmp_size = size
        while n*2 <= size
            vn,vn2,vn21 = @buf[n],@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
            if @cmp.call(vupper,vn)
                @buf[n],@buf[upper] = vupper,vn
                n = upper
            else
                break
            end
        end
#        p ret
        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{|(a1,a2),(b1,b2)|
    if a1 == b1
        a2 < b2
    else
        a1 < b1
    end
        
}
path_list.push([0,1])
gone = [false]*(n+1)
while path_list.size > 0
    cost,l = path_list.pop
    next if gone[l]
    gone[l] = true
    if l == n
        p cost
        exit
    end
    node[l].each{|r,c|
        unless gone[r]
            path_list.push([cost+c,r])
        end
    }
end
p -1
