#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  vector<long long> c(m);
  vector<long long> path(m);
  priority_queue<pair<long long, int>> ind;
  vector<pair<int, int>> ls(m);
  int maxr = 0;
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i] >> c[i];
    maxr = max(maxr, r[i]);
    ls[i].first = l[i];
    ls[i].second = i;
  }
  if (maxr < n) {
    cout << "-1\n";
    return 0;
  }
  r.push_back(1);
  sort(ls.begin(), ls.end());
  if (ls[0].first > 1) {
    cout << '-1' << '\n';
  }
  ind.push({100000000000000000LL, r.size() - 1});
  vector<int> buff;
  for (int i = 0; i < m; ++i) {
    if (i > 0) {
      if (ls[i].first != ls[i - 1].first) {
        for (int j = 0; j < buff.size(); ++j) {
          ind.push({100000000000000000LL - path[buff[j]], buff[j]});
        }
        buff.clear();
      }
    }
    while (!ind.empty() && r[ind.top().second] < ls[i].first) {
      ind.pop();
    }
    if (ind.empty()) {
      cout << "-1\n";
      return 0;
    }
    buff.push_back(ls[i].second);
    path[ls[i].second] =
        100000000000000000LL - ind.top().first + c[ls[i].second];
  }
  long long ans = 100000000000000000LL;
  for (int i = 0; i < m; ++i) {
    if (r[i] == n) {
      ans = min(ans, path[i]);
    }
  }
  cout << ans << '\n';
  return 0;
}
