#include<bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int N = 1e5 + 4;

vector<pair<int , int>> add[N];
vector<long long> er[N];
long long dp[N];
int main() {
   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
   int n , m;
   cin >> n >> m;
   for(int i = 0 ; i < m ; ++i){
      int l , r , c;
      cin >> l >> r >> c;
      add[l].push_back({r , c});
   }


   multiset<long long> ms;
   for(int i = 1 ; i <= n ; ++i){
      for(long long c : er[i])
         ms.erase(ms.find(c));
      if(i > 1)dp[i] = (ms.empty() ? -LLONG_MAX / 2 : *ms.begin());
      else dp[i] = 0;
      for(auto p : add[i]){
         ms.insert(dp[i] + p.second);
         er[p.first + 1].push_back(dp[i] + p.second);
      }
   }

   cout << (dp[n] < 0 ? -1 : dp[n]) << "\n";
}