#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <utility>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
typedef long long ll;
const ll inf = 1e+15;
int main()
{
  ll n;
  int m;
  cin >> n >> m;
  vector<pair<ll,pair<ll,ll>>> lrc(m);
  rep(i,m){
    cin >> lrc[i].first >> lrc[i].second.first >> lrc[i].second.second;
  }

  sort(lrc.begin(), lrc.end());
  
  map<ll,ll> mp1,mp2;
  mp1[1]=0;
  mp2[0]=1;
  mp1[2]=inf;
  mp2[inf]=2;


  rep(i,m){
    ll l,r,c;
    l = lrc[i].first;
    r = lrc[i].second.first;
    c = lrc[i].second.second;
    auto it1 = mp1.upper_bound(l);
    it1--;			
    ll x = it1->second + c;
    auto it2 = mp2.upper_bound(x);
    ll y = it2->second;

    if(r<y) continue;
    ll a = mp1[y];
    mp1[r+1]=mp1[y];
    mp2.erase(r+1);
    mp2[mp1[y]]=r+1;
    mp1[y]=x;
    mp2.erase(y);
    mp2[x]=y;

  }

  if(mp1[n+1]==inf) cout << (--mp1.upper_bound(n))->second << "\n";
  else cout << -1 << "\n";
  
  return 0;
}
