const INF: i64=std::i64::MAX/2;
fn main() {
    let (n,m) = {
        let t = rv::<usize>();
        (t[0], t[1])
    };
    let mut g = vec![vec![];n];
    for _ in 0..m {
        let t = rv::<usize>();
        let (l,r,c) = (t[0]-1,t[1]-1,t[2]as i64);
        g[l].push((r,c));
    }
    for i in 1..m {
        g[i].push((i-1,0));
    }
    let mut q = std::collections::BinaryHeap::new();
    q.push((INF-0,n-0));
    let mut d = vec![INF;n];
    while let Some((s,v)) = q.pop() {
        let s=INF-s;
        let v=n-v;
        if d[v]<=s {continue;}
        d[v]=s;
        if v==n-1 {break;}
        for &(u,c) in &g[v] {
            q.push((INF-(s+c),n-u));
        }
    }
    let t = d[n-1];
    println!("{}",if t<INF {t} else {-1});
}
fn rl() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s
}
fn rv<T>() -> Vec<T>
  where T: std::str::FromStr, T::Err: std::fmt::Debug, {
    rl().split_whitespace().map(|x|x.parse::<T>().unwrap()).collect()
}