#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;
  vector<vector<pair<lli, lli>>> ma(n + 10);
  vector<vector<pair<lli, lli>>> ena(n + 10);
  using p = pair<lli, lli>;
  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_pair(C[i], -R[i]));
  lli frontier = 0;
  for (auto s : ma[0]) {
    que.push(s);
  }
  for (lli i = 0; i < (n); i++) {
    for (auto s : ena[i]) {
      que.push(s);
    }
    while (!que.empty()) {
      auto s = que.top();
      que.pop();
      if ((-s.second) > i) {
        que.push(s);
        break;
      }
    }
    if (que.empty()) continue;
    auto f = que.top();
    lli right = -f.second;
    for (int j = frontier; j <= min(n - 1, -f.second); j++) {
      for (auto s : ma[j]) {
        ena[j].push_back(s);
      }
      dist[j] = min(dist[i] + f.first, dist[j]);
    }
    frontier = max(frontier, -f.second);
  }
  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;
}
