const INF: i64=std::i64::MAX/2;
fn main() {
    let (n,m) = {
        let t = rv::<usize>();
        (t[0], t[1])
    };
    let mut lrc = Vec::with_capacity(m);
    for _ in 0..m {
        let t = rv::<usize>();
        lrc.push((t[0]-1,t[1]-1,t[2]as i64));
    }
    lrc.sort();
    let f = |v| {
        let mut l=0;
        let mut r=m;
        while l<r {
            let m=(l+r)/2;
            let t=lrc[m].0;
            if t<v {
                l=m+1;
            }
            else if t>v {
                r=m;
            }
            else {
                if m==0 || lrc[m-1].0<v {
                    l=m;
                    r=m;
                }
                else {
                    r=m;
                }
            }
        }
        l
    };
    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;}
        let mut i = f(v);
        while i<m && lrc[i].0==v {
            let (_,r,c) = lrc[i];
            if r>=n-1 {
                q.push((INF-(s+c),n-(n-1)));
            }
            let mut j = std::cmp::min(m-1,f(r+1));
            while lrc[j].0>v {
                if lrc[j].0<=r {
                    let (l,r,_)=lrc[j];
                    if d[r]>s+c {
                        q.push((INF-(s+c),n-l));
                    }
                }
                if j==0 {break;}
                j-=1;
            }
            i+=1;
        }
    }
    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()
}