import strutils, sequtils, algorithm, math, sets

proc YESNO(r: bool, yes: string = "YES", no: string = "NO") =
  if r: echo yes
  else: echo no

proc getInt(): int =
  stdin.readLine.parseInt()

proc getInts(): seq[int] =
  stdin.readLine.split.map(parseInt)

proc getIntLines(lines: int): seq[int] =
  mapIt(0..(lines-1), stdin.readLine.parseInt())

proc main(): int64 =

  type Rule = tuple[L, R: int, C: int64]

  let
    NM = getInts()
    (N, M) = (NM[0], NM[1])
  var Rules = newSeq[Rule](M)
  for i in 0..M-1:
    let lrc = getInts()
    Rules[i] = (lrc[0], lrc[1], lrc[2].int64)
  Rules = Rules.sorted(proc(x, y: Rule): int = cmp(x.R, y.R))

  type DPair = tuple[i: int, d: int64]

  var ds = newSeq[DPair]()
  ds.add((1, 0.int64))

  for rule in Rules:
    let (L, R, C) = rule
    let i = ds.lowerBound((L, 0.int64), proc(x, y: DPair): int = cmp(x.i, y.i))
    if i >= ds.len: continue
    let start = ds[i]
    if start.i < L or R < start.i: continue
    var d = start.d + C
    if start.i == R:
      d = start.d

    var newPair: DPair = (R, d)
    let last = ds[ds.len - 1]
    if last.i == R:
      newPair.d = min(newPair.d, last.d)
      discard ds.pop()
    ds.add(newPair)

  let last = ds[ds.len - 1]
  if last.i != N: return -1
  return last.d

echo main()