#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, m;
  cin >> n >> m;
  pair<pair<long long, long long>, long long> v[m];
  for (long long i = 0; i < m; ++i)
    cin >> v[i].first.first >> v[i].second >> v[i].first.second;
  sort(v, v + m);
  long long d[n + 1];
  for (long long i = 0; i <= n; i++) d[i] = 1000000000000000000;
  d[1] = 0;
  set<long long> second;
  second.insert(1);
  second.insert(n);
  for (long long i = 0; i < m; ++i) {
    long long l = v[i].first.first, r = v[i].second, c = v[i].first.second;
    set<long long>::iterator itl, itr, t;
    itr = second.lower_bound(r);
    itl = second.lower_bound(l);
    long long x = d[*itl] + c;
    if (x >= d[*itr]) continue;
    t = --itr;
    while (d[*t] >= x) {
      if (t == second.begin()) {
        second.erase(t);
        break;
      }
      t--;
      second.erase(itl);
      itl = t;
    }
    second.insert(r);
    d[r] = x;
  }
  if (d[n] == 1000000000000000000) d[n] = -1;
  cout << d[n] << endl;
}
