#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
template <typename T>
using vvec = vector<vec<T>>;
int main() {
  int N, M;
  cin >> N >> M;
  vec<long long> L(M), R(M), C(M);
  vvec<int> lid(N + 1), rid(N + 1);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    lid[L[i]].push_back(i);
    rid[R[i]].push_back(i);
  }
  const long long inf = 1e18;
  vec<long long> dp(N + 1, inf);
  vec<long long> val(N + 1, inf);
  dp[1] = 0;
  map<long long, int> m;
  m[0] = 1;
  auto dec = [&](long long x) {
    assert(m.count(x));
    m[x]--;
    if (m[x] == 0) m.erase(x);
  };
  for (int i = 1; i <= N; i++) {
    if (m.empty()) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = m.begin()->first;
    for (auto& id : rid[i]) {
      if (val[id] == inf) continue;
      dec(val[id]);
    }
    for (auto& id : lid[i]) {
      m[dp[i] + C[id]]++;
      val[id] = dp[i] + C[id];
    }
    if (i == 1) dec(0);
  }
  cout << (dp[N] != inf ? dp[N] : -1) << endl;
}
