#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);
    let mut queries = vec![];
    for _ in 0..m {
        let v = read_vec::<i64>();
        let (l, r, c) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        queries.push((l, r, c));
    }
    queries.sort();
    let mut seg = LazySegmentTree::new(n);
    seg.update(0, 1, 0, 0, 0, n);
    for (l, r, c) in queries {
        let start = seg.getmin(l, l + 1, 0, 0, n);
        if start == std::i64::MAX {
            println!("-1");
            return;
        }
        seg.update(l, r + 1, start + c, 0, 0, n);
    }
    println!("{}", seg.getmin(n - 1, n, 0, 0, n));
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Debug)]
struct LazySegmentTree {
    n: usize,
    node: Vec<i64>,
    lazy: Vec<i64>,
}

impl LazySegmentTree {
    fn new(sz: usize) -> LazySegmentTree {
        let mut n = 1;
        while n < sz {
            n *= 2;
        }
        LazySegmentTree {
            n: n,
            node: vec![std::i64::MAX; n * 2],
            lazy: vec![std::i64::MAX; n * 2],
        }
    }

    fn eval(&mut self, k: usize, l: usize, r: usize) {
        if self.lazy[k] != std::i64::MAX {
            self.node[k] = min(self.node[k], self.lazy[k]);
            if r - l > 1 {
                self.lazy[2 * k + 1] = min(self.lazy[2 * k + 1], self.lazy[k]);
                self.lazy[2 * k + 2] = min(self.lazy[2 * k + 2], self.lazy[k]);
            }
            self.lazy[k] = std::i64::MAX;
        }
    }

    fn update(&mut self, a: usize, b: usize, x: i64, k: usize, l: usize, r: usize) {
        // update [l, r) k = 0, l = 0, r = n is default
        self.eval(k, l, r);

        // if out of range, do nothing.
        if b <= l || r <= a {
            return;
        }

        if a <= l && r <= b {
            self.lazy[k] = min(self.lazy[k], x);
            self.eval(k, l, r);
        } else {
            self.update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            self.update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            self.node[k] = min(self.node[2 * k + 1], self.node[2 * k + 2]);
        }
    }

    fn getmin(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> i64 {
        if b <= l || r <= a {
            return std::i64::MAX;
        }

        self.eval(k, l, r);
        if a <= l && r <= b {
            return self.node[k];
        }

        min(
            self.getmin(a, b, 2 * k + 1, l, (l + r) / 2),
            self.getmin(a, b, 2 * k + 2, (l + r) / 2, r),
        )
    }
}
