macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
#[doc = " RMaxQ & RUpdQ M1=max F1=M2=subs"]
#[doc = " RMinQ & RAddQ M1=min F1=M2=add"]
#[doc = " RSumQ & RAddQ M1=F1=M2=add                  F2=a*b"]
#[doc = " RSumQ & RUpdQ M1=add F1=M2=subs             F2=a*b"]
#[doc = " RSumQ & RXorQ M1=add F1=(b>=1?b-a:a) M2=a^b F2=a*b"]
pub struct LazySegmentTree<
    M1: Monoid,
    M2: Monoid,
    F1: Fn(&M1::T, &M2::T) -> M1::T,
    F2: Fn(&M2::T, usize) -> M2::T,
> {
    n: usize,
    seg: Vec<M1::T>,
    lazy: Vec<M2::T>,
    m1: M1,
    m2: M2,
    op: F1,
    p: F2,
}
impl<M1: Monoid, M2: Monoid, F1: Fn(&M1::T, &M2::T) -> M1::T, F2: Fn(&M2::T, usize) -> M2::T>
    LazySegmentTree<M1, M2, F1, F2>
{
    pub fn new(n: usize, m1: M1, m2: M2, op: F1, p: F2) -> LazySegmentTree<M1, M2, F1, F2> {
        let n = 1 << format!("{:b}", n - 1).len();
        let seg = vec![m1.mempty(); 2 * n - 1];
        let lazy = vec![m2.mempty(); 2 * n - 1];
        LazySegmentTree {
            n: n,
            seg: seg,
            lazy: lazy,
            m1: m1,
            m2: m2,
            op: op,
            p: p,
        }
    }
    pub fn from_vec(
        v: Vec<M1::T>,
        m1: M1,
        m2: M2,
        op: F1,
        p: F2,
    ) -> LazySegmentTree<M1, M2, F1, F2> {
        let n = 1 << format!("{:b}", v.len() - 1).len();
        let mut seg = vec![m1.mempty(); 2 * n - 1];
        for (i, x) in v.into_iter().enumerate() {
            seg[i + n - 1] = x;
        }
        for i in (0..n - 1).rev() {
            seg[i] = m1.mappend(&seg[2 * i + 1], &seg[2 * i + 2]);
        }
        let lazy = vec![m2.mempty(); 2 * n - 1];
        LazySegmentTree {
            n: n,
            seg: seg,
            lazy: lazy,
            m1: m1,
            m2: m2,
            op: op,
            p: p,
        }
    }
    pub fn eval(&mut self, k: usize, len: usize) {
        if self.lazy[k] != self.m2.mempty() {
            if k * 2 + 1 < self.n * 2 - 1 {
                self.lazy[2 * k + 1] = self.m2.mappend(&self.lazy[2 * k + 1], &self.lazy[k]);
                self.lazy[2 * k + 2] = self.m2.mappend(&self.lazy[2 * k + 2], &self.lazy[k]);
            }
            self.seg[k] = (self.op)(&self.seg[k], &(self.p)(&self.lazy[k], len));
            self.lazy[k] = self.m2.mempty();
        }
    }
    fn update_inner(
        &mut self,
        l: usize,
        r: usize,
        x: M2::T,
        k: usize,
        a: usize,
        b: usize,
    ) -> M1::T {
        self.eval(k, b - a);
        if b <= l || r <= a {
            self.seg[k].clone()
        } else if l <= a && b <= r {
            self.lazy[k] = self.m2.mappend(&self.lazy[k], &x);
            (self.op)(&self.seg[k], &(self.p)(&self.lazy[k], b - a))
        } else {
            let lx = self.update_inner(l, r, x.clone(), k * 2 + 1, a, (a + b) / 2);
            let rx = self.update_inner(l, r, x, k * 2 + 2, (a + b) / 2, b);
            let res = self.m1.mappend(&lx, &rx);
            self.seg[k] = res.clone();
            res
        }
    }
    pub fn update(&mut self, l: usize, r: usize, x: M2::T) -> M1::T {
        let n = self.n;
        self.update_inner(l, r, x, 0, 0, n)
    }
    fn query_inner(&mut self, l: usize, r: usize, k: usize, a: usize, b: usize) -> M1::T {
        self.eval(k, b - a);
        if b <= l || r <= a {
            self.m1.mempty()
        } else if l <= a && b <= r {
            self.seg[k].clone()
        } else {
            let lx = self.query_inner(l, r, k * 2 + 1, a, (a + b) / 2);
            let rx = self.query_inner(l, r, k * 2 + 2, (a + b) / 2, b);
            self.m1.mappend(&lx, &rx)
        }
    }
    pub fn query(&mut self, l: usize, r: usize) -> M1::T {
        let n = self.n;
        self.query_inner(l, r, 0, 0, n)
    }
}
pub struct MinMonoid<T: Ord + Clone> {
    min: T,
}
impl<T: Ord + Clone> MinMonoid<T> {
    pub fn new(min: T) -> MinMonoid<T> {
        MinMonoid { min: min }
    }
}
impl<T: Ord + Clone> Monoid for MinMonoid<T> {
    type T = T;
    fn mempty(&self) -> T {
        self.min.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        std::cmp::min(x, y).clone()
    }
}
const INF: usize = std::usize::MAX;
fn main() {
    input! { n, m, mut lrc: [(usize1, usize1, usize); m] };
    lrc.sort();
    let mut seg = LazySegmentTree::new(
        n,
        MinMonoid::new(INF),
        MinMonoid::new(INF),
        |&x, &y| std::cmp::min(x, y),
        |&x, _y| x,
    );
    seg.update(0, 1, 0);
    for &(l, r, c) in &lrc {
        let x = seg.query(l, r + 1);
        if x < INF {
            seg.update(l, r + 1, x + c);
        }
    }
    let ans = seg.query(n - 1, n);
    println!("{}", if ans >= INF { -1 } else { ans as i64 });
}


Measured memory_bytes: 15608