class PQueue
    def initialize(&block)
        @buf = [nil]
        @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]
            if @cmp.call(val,vnextn)
                @buf[n] = vnextn
                n = nextn
            else
                break
            end
        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
                if @cmp.call(vupper,vn)
                    @buf[n] = vupper
                    n = upper
                else
                    break
                end
            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{|(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] = cost
    break if n == l
    node[l].each{|r,c|
        unless gone[r]
            path_list.push([cost+c,r])
        end
    }
end
p gone[n]||-1
