#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;
  map<lli, vector<pair<lli, lli>>> ma;
  using p = pair<lli, lli>;
  lli cur = 0;
  vector<lli> dist(n, 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 (lli i = 0; i < (n); i++) {
    for (auto s : ma[i]) {
      que.push(s);
    }
    while (!que.empty()) {
      auto s = que.top();
      que.pop();
      if (s.second > i) {
        que.push(s);
        break;
      }
    }
    auto f = que.top();
    for (int j = frontier; j <= f.second; j++) {
      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() {
  int a;
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &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++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
    scanf("%lld", &C[i]);
    L[i]--, R[i]--;
  }
  solve(N, M, std::move(L), std::move(R), std::move(C));
  return 0;
}
