Problem:
Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28

Solution in LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
      self.lazy[i][j] = false
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  self.lazy = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
    self.lazy[stagenum] = {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  for i = 1, mul do self.lazy[stagenum][i] = false end
  self:updateAll()
end
SegTree.getValue = function(self, idx)
  return self.stage[self.stagenum][idx]
end
SegTree.setRange = function(self, left, right, value)
  if left == right then self.stage[self.stagenum][left] = value return end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      if value < self.stage[stage][mce(l / sz)] then
        self.stage[stage][mce(l / sz)] = value
        self.lazy[stage][mce(l / sz)] = true
      end
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
end
SegTree.resolve = function(self, idx)
  for stage = 1, self.stagenum - 1 do
    local pos = mce(idx / (self.size[stage]))
    if self.lazy[stage][pos] then
      self.lazy[stage][pos] = false
      local v = self.stage[stage][pos]
      if v < self.stage[stage + 1][pos * 2 - 1] then
        self.stage[stage + 1][pos * 2 - 1] = v
        self.lazy[stage + 1][pos * 2 - 1] = true
      end
      if v < self.stage[stage + 1][pos * 2] then
        self.stage[stage + 1][pos * 2] = v
        self.lazy[stage + 1][pos * 2] = true
      end
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local t = {}
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  t[i] = {a, b, c}
end

table.sort(t, function(x, y) return x[1] < y[1] end)
local st = SegTree.new(n, mmi, 99999999999999)
st:setRange(1, 1, 0)
for i = 1, m do
  st:resolve(t[i][1])
  local lval = st:getValue(t[i][1])
  st:setRange(t[i][1] + 1, t[i][2], lval + t[i][3])
end
st:resolve(n)
print(99999999999999 <= st:getValue(n) and -1 or st:getValue(n))