#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)1e5 + 9;
vector<pair<long long, long long>> L[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  int l, r, c;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r >> c;
    L[l].push_back(make_pair(r, c));
  }
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      ff;
  ff.push(make_pair(0ll, 1));
  int mx;
  for (int i = 1; i <= n; i++) {
    while (!ff.empty() && i > ff.top().second) {
      ff.pop();
    }
    if (ff.empty()) {
      cout << "-1\n";
      return 0;
    }
    mx = ff.top().first;
    if (i == n) {
      cout << mx << "\n";
      return 0;
    }
    for (auto x : L[i]) {
      ff.push(make_pair(mx + x.second, x.first));
    }
  }
  return 0;
}
