#include <bits/stdc++.h>

using namespace std;
using P = pair<long, long>;

typedef long long int ll;

#define EPS (1e-7)
#define INF (1e18)
#define PI (acos(-1))

#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvinv(xs) sort(xs.begin(), xs.end(), greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define index_of(as, x) \
  distance(as.begin(), lower_bound(as.begin(), as.end(), x))
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define concat(xs, ys) (xs).insert((xs).end(), (ys).begin(), (ys).end())

// const int mod = 1000000007;
//  const int mod = 998244353;

struct edge {
  long l;
  long r;
  long c;

  bool operator>(edge other) const {
    return l > other.l || (l == other.l && c > other.c);
  }
};

template <typename T,typename E>
struct SegmentTree {
  typedef function<T(T,T)> F;
  typedef function<T(T,E)> G;
  typedef function<E(E,E)> H;
  typedef function<E(E,int)> P;
  int n;
  F f;
  G g;
  H h;
  P p;
  T d1;
  E d0;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(
      int n_,
      F f,
      G g,
      H h,
      T d1,
      E d0,
	    vector<T> v=vector<T>(),
      P p=[](E a, int b){ return a; }
    ): f(f),g(g),h(h),d1(d1),d0(d0),p(p) {
      init(n_);
      if(n_==(int)v.size()) build(n_,v);
  }

  void init(int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1,d1);
    laz.clear();
    laz.resize(2*n-1,d0);
  }

  void build(int n_, vector<T> v){
    for(int i=0;i<n_;i++) dat[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--)
      dat[i]=f(dat[i*2+1],dat[i*2+2]);
  }

  inline void eval(int len,int k){
    if(laz[k]==d0) return;
    if(k*2+1<n*2-1){
      laz[k*2+1]=h(laz[k*2+1],laz[k]);
      laz[k*2+2]=h(laz[k*2+2],laz[k]);
    }
    dat[k]=g(dat[k],p(laz[k],len));
    laz[k]=d0;
  }

  T update(int a,int b,E x,int k,int l,int r){
    eval(r-l,k);
    if(r<=a||b<=l) return dat[k];
    if(a<=l&&r<=b){
      laz[k]=h(laz[k],x);
      return g(dat[k],p(laz[k],r-l));
    }
    return dat[k]=f(update(a,b,x,k*2+1,l,(l+r)/2),
		    update(a,b,x,k*2+2,(l+r)/2,r));
  }

  T update(int a,int b,E x){
    return update(a,b,x,0,0,n);
  }

  T query(int a,int b,int k,int l,int r){
    eval(r-l,k);
    if(r<=a||b<=l) return d1;
    if(a<=l&&r<=b) return dat[k];
    T vl=query(a,b,k*2+1,l,(l+r)/2);
    T vr=query(a,b,k*2+2,(l+r)/2,r);
    return f(vl,vr);
  }

  T query(int a,int b){
    return query(a,b,0,0,n);
  }
};

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  priority_queue<edge, vector<edge>, greater<edge>> P;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    P.push(edge{l, r, c});
  }

  vector<long> d(n);
  rep(i, n) {
    d[i] = INF;
  }
  d[0] = 0;

  SegmentTree<long, long> st(
    n,
    [](long x, long y) {
      return x+y;
    },
    [](long x, long y) {
      return min(x, y);
    },
    [](long x, long y) {
      return min(x, y);
    },
    0,
    INF,
    d
  );
  
  while(!P.empty()) {
    edge E = P.top();
    P.pop();

    if (st.query(E.l, E.l+1) + E.c >= st.query(E.r, E.r+1)) continue;

    st.update(E.l, E.r+1, st.query(E.l, E.l+1) + E.c);
  }

  cout << (st.query(n-1, n) == INF ? - 1 : st.query(n-1, n)) << endl;
}


Measured memory_bytes: 9584