#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
#define Priq priority_queue<int,vi,greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll<<60)
signed main(){
  int N,M; cin>>N>>M;
  vector<pair<pii,int>> A(M);
  rep(i,0,M) cin>>A[i].first.second>>A[i].first.first>>A[i].second;
  sort(ALL(A));
  set<pii> S;
  S.insert(mp(1,0));
  rep(i,0,M){
    auto p=*rbegin(S);
    if(p.first>=A[i].first.second){
      auto q=S.lower_bound(mp(A[i].first.second,0));
      auto r=*q;
      int X=r.second+A[i].second;
      auto t=*S.lower_bound(mp(A[i].first.first,0));
      if(t.second>X){
        auto it=S.begin();
        while(it!=S.end()){
          auto s=*it;
          if(A[i].first.second<=s.first&&s.second>=X) S.erase(s);
          it++;
        }
      S.insert(mp(A[i].first.first,X));
      }
    }
  }
  auto p=*rbegin(S);
  if(p.first==N) cout<<p.second<<endl;
  else cout<<-1<<endl;
  }

