let max_int = 1145141919810

type t =
  | Leaf
  | Node of body
and body = { left : t; right : t; data : int; l : int; r : int }

let rec init l xs = function
  | 1 ->
      Node
        { left = Leaf;
          right = Leaf;
          data = max_int; l;
          r = List.hd xs }, xs
  | n ->
      let (left, m :: ys) = init l xs (n / 2) in
      let (right, (r :: _ as zs)) = init m ys ((n + 1) / 2) in
      Node { left; right; data = max_int; l; r }, zs
let init (x :: xs) = fst @@ init x xs (List.length xs)

let rec update a b x = function
  | Leaf -> Leaf
  | Node ({ left; right; data; l; r } as t) ->
      if b <= l || r <= a || t.data <= x
      then Node t
      else begin
        if a <= l && r <= b
        then Node { t with data = min x t.data }
        else Node { t with left = update a b x left; right = update a b x right }
      end

let rec query acc i = function
  | Leaf -> acc
  | Node { left; right; data; l; r } ->
      if i < l || r <= i
      then acc
      else query (query (min acc data) i left) i right
let query = query max_int

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let lrcs = Array.init m @@ fun _ -> Scanf.scanf "%d %d %d\n" @@ fun l r c -> l, r, c in
  Array.sort (fun (l, r, _) (l', r', _) -> compare (l', r) (l, r')) lrcs;
  let segtree = 
    Array.fold_right (fun (l, r, c) segtree -> update l (r + 1) (c + query l segtree) segtree) lrcs @@
    update 1 2 0 @@
    init @@
    List.sort_uniq compare @@
    Array.fold_left (fun acc (l, r, _) -> l :: r + 1 :: acc) [1; 2; n] lrcs in
  Printf.printf "%d\n" @@ if query n segtree = max_int then -1 else query n segtree
