#include <bits/stdc++.h>
using namespace std;
const long long N = 100009, inf = 1000000000000000000;
long long n, m;
struct edge {
  long long l, r, c;
  bool operator<(const edge& other) const { return r > other.r; }
} e[N];
void nope() {
  cout << -1 << endl;
  exit(0);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    cin >> e[i].l >> e[i].r >> e[i].c;
  }
  sort(e, e + n);
  priority_queue<pair<long long, long long>> pq;
  long long ind = 0;
  long long cur = 0;
  for (long long i = n - 1; i > 0; --i) {
    while (ind < m && e[ind].r > i) {
      pq.emplace(-e[ind].c - cur, e[ind].l);
      ind++;
    }
    while (!pq.empty() && pq.top().second > i) {
      pq.pop();
    }
    if (pq.empty()) nope();
    cur = -pq.top().first;
  }
  cout << cur << endl;
  return 0;
}
