#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
void solve(long long n, long long m, std::vector<long long> L,
           std::vector<long long> R, std::vector<long long> C) {
  lli inf = 1e18;
  using p = tuple<lli, lli, lli>;
  vector<vector<p>> ma(n + 10);
  vector<lli> fua;
  for (lli i = 0; i < (m); i++) fua.push_back(L[i]);
  sort(fua.begin(), fua.end());
  vector<lli> dist(n + 10, inf);
  dist[0] = 0;
  priority_queue<p, vector<p>, greater<p>> que;
  for (lli i = 0; i < (m); i++) ma[L[i]].push_back(make_tuple(C[i], -R[i], i));
  lli frontier = 0;
  for (lli i = 0; i < (n); i++) {
    for (auto s : ma[i]) {
      que.push(s);
    }
    lli a, b, c;
    while (!que.empty()) {
      auto s = que.top();
      que.pop();
      tie(a, b, c) = s;
      if (R[c] > i) {
        que.push(s);
        break;
      }
    }
    if (que.empty()) continue;
    auto f = que.top();
    tie(a, b, c) = f;
    lli right = R[c];
    for (int j = i; j <= *lower_bound(fua.begin(), fua.end(), i); j++) {
      dist[j] = min(dist[L[c]] + a, dist[j]);
    }
  }
  cout << ((dist[n - 1] >= inf) ? -1 : dist[n - 1]) << endl;
}
int main() {
  long long N;
  cin >> N;
  long long M;
  cin >> M;
  std::vector<long long> L(M);
  std::vector<long long> R(M);
  std::vector<long long> C(M);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--, R[i]--;
  }
  solve(N, M, L, R, C);
  return 0;
}
