#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = (int)1e5 + 9;
vector<pii> L[N];

int main(){
  fastIO;
  int n, m;
  cin >> n >> m;
  int l, r, c;
  for(int i = 1; i <= m; i ++ ){
    cin >> l >> r >> c;
    L[l].push_back(mp(r, c));
  }
  priority_queue<pii, vector<pii>, greater<pii>> ff;
  ff.push(mp(0ll, 1));
  ll mx;
  for(int i = 1; i <= n; i ++ ){
    while(!ff.empty() && i > ff.top().se){
      ff.pop();
    }
    if(ff.empty()){
      cout << "-1\n";
      return 0;
    }
    mx = ff.top().fi;
    if(i == n){
      cout << mx << "\n";
      return 0;
    }
    for(auto x : L[i]){
      ff.push(mp(mx + x.se, x.fi));
    }
  }
  return 0;
}