#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  ll N, M;
  cin>>N>>M;
  vector<tuple<ll, ll, ll> > arr(M);
  ll L, R;
  ll C;
  for(ll i=0;i<M;++i) {
    cin>>L>>R>>C;
    L--;
    R--;
    arr[i] = make_tuple(L, C, R);
  }
  sort(arr.begin(), arr.end());
  vector<ll> ans(N);
  for(ll i=0;i<N;++i) ans[i] = big;
  ans[0] = 0;
  ll index;
  for(ll i=0;i<M;++i) {
    L = get<0>(arr[i]);
    C = get<1>(arr[i]);
    R = get<2>(arr[i]);
    index = lower_bound(ans.begin()+L, ans.begin()+R, ans[L]+C) - ans.begin();
    for(ll j=index;j<=R;++j) {
      ans[j] = min(ans[j], ans[L]+C);
    }
  }
  if(ans[N-1]<big) cout<<ans[N-1]<<endl;
  else cout<<-1<<endl;
}

