#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll INF = 1e18;
template <class T>
void print_container(T& container, string delim = " ") {
  bool first = true;
  for (auto& t : container) {
    if (!first) {
      cout << delim;
    }
    first = false;
    cout << t;
  }
  cout << endl;
}
struct Data {
  ll cost;
  int to;
};
bool operator<(const Data& lhs, const Data& rhs) {
  if (lhs.cost != rhs.cost)
    return lhs.cost < rhs.cost;
  else {
    return lhs.to > rhs.to;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> retu(n + 1);
  vector<Data> aiueo(n + 1, {INF, -1});
  vector<Data> dat(m);
  for (int((i)) = (0); ((i)) < ((m)); ((i))++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    dat[i] = {c, r};
    retu[l].push_back(i + 1);
    retu[r].push_back(-(i + 1));
  }
  set<Data> se;
  set<Data> ng;
  for (int((i)) = (0); ((i)) < ((n)); ((i))++) {
    for (auto t : retu[i]) {
      if (t > 0) {
        se.insert(dat[t - 1]);
      } else {
        ng.insert(dat[-t - 1]);
      }
    }
    while (!se.empty()) {
      auto dd = se.begin();
      if (ng.count(*dd)) {
        se.erase(se.begin());
      } else
        break;
    }
    if (se.size()) aiueo[i] = *(se.begin());
  }
  int now = 0;
  ll cost = 0;
  while (now < n - 1) {
    auto to = aiueo[now];
    if (to.cost == INF) {
      cout << -1 << endl;
      return 0;
    }
    cost += to.cost;
    now = to.to;
  }
  cout << cost << endl;
  return 0;
}
