#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>, long long 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());
  long long int ans = 0, t = 1;
  while (t != n) {
    int minc = INF, minr = -1;
    for (int i = 0; i < m; i++) {
      if (p[i].first.first > t) break;
      if (minc > p[i].second && p[i].first.second > t) {
        minc = p[i].second;
        minr = p[i].first.second;
      }
    }
    if (minr == -1) {
      cout << -1;
      return 0;
    }
    ans += minc;
    t = minr;
  }
  cout << ans;
}
