#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
void Nikkei2D() {
  int N, M;
  cin >> N >> M;
  multimap<int, pair<int, int> > src;
  for (int i = 0; i < M; ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    src.insert({L, {R, C}});
  }
  map<int, ll> dist;
  map<ll, int> dist2;
  dist[1] = 0;
  dist2[0] = 1;
  for (auto& x : src) {
    int start = x.first;
    int end = x.second.first;
    ll d = x.second.second;
    auto p = dist.lower_bound(start);
    if (p == dist.cend()) {
      cout << -1 << endl;
      return;
    }
    ll y = p->second + d;
    auto q = dist2.lower_bound(y);
    if (q == dist2.cend()) {
      auto r = dist2.rbegin();
      if (r->second < end) {
        dist2[y] = end;
        dist[end] = y;
      }
      continue;
    }
    if (q != dist2.begin()) {
      auto r = q;
      --r;
      if (r->second >= end) {
        continue;
      }
    }
    if (q->second >= end) {
      dist2[y] = end;
      dist[end] = y;
    } else {
      auto z = dist.find(q->second);
      vector<ll> tmp;
      auto qq = z;
      for (; qq != dist.cend(); ++qq) {
        if (qq->first > end) break;
        tmp.push_back(qq->second);
      }
      dist.erase(z, qq);
      dist2.erase(dist2.find(tmp.front()), dist2.find(tmp.back()));
      dist2[end] = y;
      dist[y] = end;
    }
  }
  cout << dist.find(N)->second << endl;
}
int main() {
  Nikkei2D();
  return 0;
}
