#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long n, m;
  cin >> n >> m;
  vector<tuple<long, long, long>> v(m);
  for (auto &x : v) {
    long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    x = make_tuple(l, r, c);
  }
  sort(v.begin(), v.end());
  set<pair<long, long>> w;
  w.insert({0, 0});
  w.insert({1e9, 1e9});
  for (auto &x : v) {
    long l, r, c;
    tie(l, r, c) = x;
    auto t = w.lower_bound(make_pair(l, 0l));
    auto s = w.lower_bound(make_pair(r, 0l));
    if (t->first == 1e9) {
      cout << -1 << endl;
      return 0;
    }
    if (s->second > t->second) {
      w.insert({r, t->second + c});
    }
  }
  auto t = w.lower_bound(make_pair(n - 1, 0l));
  if (t->first == 1e9) {
    cout << -1 << endl;
    return 0;
  }
  cout << t->second << endl;
}
