#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
const long long int INF = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int>> p(m);
  for (int i = 0; i < m; i++)
    cin >> p[i].first.first >> p[i].first.second >> p[i].second;
  sort(p.begin(), p.end());
  priority_queue<pair<int, pair<int, int>>> q;
  long long int ans = 0, t = 1, i = 0;
  while (t != n) {
    for (i = i; i < m; i++) {
      if (p[i].first.first > t) break;
      q.push({-p[i].second, {p[i].first.first, p[i].first.second}});
    }
    while (!q.empty()) {
      if (q.top().second.second <= t)
        q.pop();
      else
        break;
    }
    if (q.empty()) {
      cout << -1;
      return 0;
    }
    ans += -q.top().first;
    t = q.top().second.second;
    q.pop();
  }
  cout << ans;
}
