#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
vector<pair<ll, ll>> ve[(ll)(2e5 + 4)];
int main() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    ve[l].push_back(make_pair(c, r));
  }
  ll ne = 1;
  set<pair<ll, ll>> sp;
  ll co = 0;
  for (ll i = 1; i <= n; i++) {
    while (sp.size() && sp.begin()->second < i) {
      sp.erase(sp.begin());
    }
    for (auto x : ve[i]) {
      sp.insert(x);
    }
    if (i < ne) {
      continue;
    }
    if (sp.size() == 0 && i >= ne) {
      cout << "-1\n";
      return 0;
    }
    co += sp.begin()->first;
    ll mi = sp.begin()->first;
    while (sp.size() && sp.begin()->first == mi) {
      if (sp.begin()->second > i) {
        ne = sp.begin()->second;
      }
      sp.erase(sp.begin());
    }
    if (ne == n) {
      break;
    }
  }
  cout << co << "\n";
}
