#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<pair<pair<long long, long long>, long long>> v;
set<pair<long long, pair<long long, long long>>> s;
long long a[123456], p = 1;
signed main() {
  a[1] = 0;
  cin >> n >> m;
  for (long long i = 0; i < (m); i++) {
    pair<pair<long long, long long>, long long> p1;
    cin >> p1.first.first >> p1.first.second >> p1.second;
    v.push_back(p1);
  }
  sort(v.begin(), v.end());
  long long q = 0;
  while (p < n) {
    while (v[q].first.first <= p) {
      v[q].second += a[v[q].first.first];
      s.insert(make_pair(v[q].second, v[q].first));
      q++;
    }
    if (s.size() == 0) {
      cout << "-1\n";
      return 0;
    }
    pair<long long, pair<long long, long long>> p1 = *s.begin();
    s.erase(s.begin());
    for (long long j = p + 1; j <= p1.second.second; j++) a[j] = p1.first;
    p = p1.second.second;
  }
  cout << a[n] << endl;
}
