use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

fn main(){
    let line = read_as_vec::<usize>();

    let (n, m) = (line[0], line[1]);

    let mut dp = vec![std::u64::MAX;n];
    dp[0] = 0;
    let mut ops = vec![];
    for _ in 0..m{
        let line = read_as_vec::<usize>();
        ops.push((line[0]-1, line[1]-1, line[2] as u64));
    }

    ops.sort();
    for op in ops{
        let (l, r, c) = (op.0, op.1, op.2);
        if dp[l] != std::u64::MAX{
            for i in l+1..r+1{
                dp[i] = cmp::min(dp[i], dp[l] + c);
            }
        }
    }
    if dp[n-1] == std::u64::MAX{
        println!("-1");
    }else{
        println!("{:?}", dp[n-1]);
    }
}
