use std::collections::BinaryHeap;
fn solve() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();
    let m: usize = sc.read();
    let mut lrc: Vec<(usize, usize, i64)> = (0..m)
        .map(|_| {
            let l: usize = sc.read();
            let r: usize = sc.read();
            let c: i64 = sc.read();
            (l - 1, r - 1, c)
        })
        .collect();
    lrc.sort();
    let mut heap = BinaryHeap::new();
    heap.push((0, 0, 0));
    let mut max_id = 0;
    let mut results = vec![None; n + 1];
    while let Some(res) = heap.pop() {
        let cost = -res.0;
        let pos = res.1;
        let idx = res.2;
        if results[pos].is_some() {
            continue;
        }
        results[pos] = Some(cost);
        max_id = std::cmp::max(max_id, idx);
        for i in max_id..m {
            let (l, r, c) = lrc[i];
            if l <= pos && pos < r {
                heap.push((-(cost + c), r, i + 1));
            } else {
                break;
            }
        }
    }
    let result = if results[n - 1].is_some() {
        results[n - 1].unwrap()
    } else {
        -1
    };
    println!("{}", result);
}

fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024) // 64MB
        .spawn(|| solve())
        .unwrap()
        .join()
        .unwrap();
}

//snippet from kenkoooo
pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_line(&mut self) -> String {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b'\n' || b == b'\r')
            .take_while(|&b| b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
