#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
long long n, m, l[100008], r[100008], c[100008];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long>> v;
  for (long long i = (1); i <= (m); i++) {
    cin >> l[i] >> r[i] >> c[i];
    v.push_back(make_pair(pair<long long, long long>(l[i], r[i]), c[i]));
  }
  sort(v.begin(), v.end());
  set<pair<long long, long long>> s;
  s.insert(pair<long long, long long>(1, 0));
  for (long long i = (0); i <= (m - 1); i++) {
    auto it = s.lower_bound(pair<long long, long long>((v[i].first).first, 0));
    if (it != s.end()) {
      auto buf = *it;
      s.insert(pair<long long, long long>((v[i].first).second,
                                          buf.second + v[i].second));
    }
  }
  auto ans_it = s.lower_bound(pair<long long, long long>(n, 0));
  if (ans_it != s.end()) {
    auto ans_buf = *ans_it;
    cout << (ans_buf.second) << endl;
  } else {
    cout << (-1) << endl;
  }
  return 0;
}
