#include <bits/stdc++.h>
using namespace std;
long long inf = (long long)1e18;
vector<pair<int, int> > X[100005];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    Q;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
  int N, M;
  cin >> N >> M;
  while (M--) {
    int L, R, C;
    cin >> L >> R >> C;
    X[L].push_back({R, C});
  }
  int ptr = 2;
  long long ans = 0;
  for (auto x : X[1]) Q.push({x.second, x.first});
  while (ptr <= N && !Q.empty()) {
    pair<int, int> pr = Q.top();
    Q.pop();
    ans += pr.first;
    while (ptr <= N && ptr <= pr.second) {
      for (auto x : X[ptr]) Q.push({x.second, x.first});
      ptr++;
    }
    while (!Q.empty() && Q.top().second < ptr) Q.pop();
  }
  if (ptr <= N) ans = -1;
  cout << ans << "\n";
  return 0;
}
