#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m), c(m);
  vector<vector<int>> beg;
  vector<vector<int>> end;
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
    beg[l[i]].push_back(i);
    end[r[i]].push_back(i);
  }
  vector<long long int> d(n, LONG_MAX);
  d[0];
  set<pair<long long int, int>> now;
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      if (now.empty()) {
        cout << -1;
        return 0;
      }
      d[i] = now.begin()->first;
    }
    for (int x : beg[i]) {
      now.insert({d[l[x]] + c[x], x});
    }
    for (int x : end[i]) {
      now.erase({d[l[x]] + c[x], x});
    }
  }
  cout << d[n - 1] << "\n";
  return 0;
}
