#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  pair<pair<long long, long long>, long long> inp[m];
  for (auto &i : inp) {
    cin >> i.first.first >> i.first.second >> i.second;
    --i.first.first;
  }
  sort(inp, inp + n);
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      Q;
  Q.push({0, 1});
  for (auto &i : inp) {
    while (!Q.empty() && Q.top().second <= i.first.first) Q.pop();
    if (Q.empty()) {
      cout << -1;
      return 0;
    }
    Q.push({Q.top().first + i.second, i.first.second});
  }
  while (!Q.empty() && Q.top().second < n) Q.pop();
  if (Q.empty()) {
    cout << -1;
    return 0;
  }
  cout << Q.top().first;
  return 0;
}
