let () =
    let main () =
        let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
        let lrc = Array.init m (fun _ -> Scanf.sscanf (read_line()) "%d %d %d" (fun l r c -> l, r, c)) in
        Array.sort compare lrc;
        let mm = 1_000_000_000_000_000_000 in
        let dist = Array.make (n + 2) mm in
        dist.(1) <- 0; 
        Array.iter (fun (l, r, c) ->
            let k = dist.(l) + c in
            for i = l + 1 to r do
                if dist.(i) > k then dist.(i) <- k
            done) lrc;
        Printf.printf "%d\n" (if dist.(n) = mm then (-1) else dist.(n))
    in
    main ()