use std::cmp::{Ord, Ordering, PartialOrd};
use std::io::{stdin, Read};
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
pub struct Reverse<T>(pub T);

impl<T: PartialOrd> PartialOrd for Reverse<T> {
    #[inline]
    fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }

    #[inline]
    fn lt(&self, other: &Self) -> bool {
        other.0 < self.0
    }
    #[inline]
    fn le(&self, other: &Self) -> bool {
        other.0 <= self.0
    }
    #[inline]
    fn gt(&self, other: &Self) -> bool {
        other.0 > self.0
    }
    #[inline]
    fn ge(&self, other: &Self) -> bool {
        other.0 >= self.0
    }
}

impl<T: Ord> Ord for Reverse<T> {
    #[inline]
    fn cmp(&self, other: &Reverse<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let (n, m): (usize, usize) = parse!(it, 2);
    let mut e = vec![];
    let mut g = vec![vec![]; n];
    for _ in 0..m {
        let (l, r, c): (usize, usize, i64) = parse!(it, 3);
        e.push((l - 1, r - 1));
        g[l - 1].push((r - 1, c));
    }
    e.sort();
    for i in 0..(e.len() - 1) {
        if e[i].1 < e[i + 1].0 {
            println!("{}", -1);
            return;
        }
    }
    for i in 0..(n - 1) {
        g[i + 1].push((i, 0));
    }

    let mut q = std::collections::BinaryHeap::new();
    let mut d = vec![std::i64::MAX / 10; n];
    d[0] = 0;
    for i in 0..n {
        q.push((Reverse(d[i]), i));
    }
    while let Some((Reverse(du), u)) = q.pop() {
        for &(v, c) in g[u].iter() {
            if d[v] > du + c {
                d[v] = du + c;
                q.push((Reverse(d[v]), v));
            }
        }
    }
    println!("{}", d[n - 1]);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}
