#include <bits/stdc++.h>
using namespace std;
long long N, M;
vector<tuple<long long, long long, long long>> E;
long long solve() {
  long long v = 0, d = 0, e = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  while (v != N - 1) {
    for (; get<0>(E[e]) <= v; ++e) {
      long long l, r, c;
      tie(l, r, c) = E[e];
      que.push(make_pair(c, r));
    }
    if (que.empty()) return -1;
    long long c, r;
    tie(c, r) = que.top();
    que.pop();
    d += c;
    v = r;
  }
  return d;
}
int main() {
  cin >> N >> M;
  for (long long i = 0; i < M; ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    E.push_back(make_tuple(l, r, c));
  }
  sort(E.begin(), E.end());
  cout << solve() << endl;
  return 0;
}
