#include <bits/stdc++.h>
using namespace std;
template <class T>
using VV = vector<vector<T>>;
template <class T>
inline T SMIN(T& a, const T b) {
  return a = (a > b) ? b : a;
}
template <class T>
inline T SMAX(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class Iter>
void __kumaerrc(Iter begin, Iter end) {
  for (; begin != end; ++begin) {
    cerr << *begin << ',';
  }
  cerr << endl;
}
void __kumaerr(istream_iterator<string> it) {
  (void)it;
  cerr << endl;
}
template <typename T, typename... Args>
void __kumaerr(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << "=" << a << ", ", __kumaerr(++it, args...);
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& _os, const std::pair<S, T>& _p) {
  return _os << "{" << _p.first << ',' << _p.second << "}";
}
const int MAX_N = 1e6 + 1;
long long A[MAX_N], L[MAX_N], R[MAX_N], W[MAX_N];
int N, M;
const long long Inf = 1e17;
void solve() {
  using T = tuple<int, int, long long>;
  vector<T> X;
  for (int i = 0; i < (M); ++i) X.emplace_back(R[i], L[i], W[i]);
  sort((X).begin(), (X).end());
  map<int, long long> D;
  D[1] = 0;
  for (int i = 0; i < (M); ++i) {
    int l, r;
    long long w;
    tie(r, l, w) = X[i];
    auto it = D.lower_bound(l);
    if (it != D.end()) {
      if (D.count(r) == 0) D[r] = Inf;
      SMIN(D[r], it->second + w);
    }
  }
  long long res = D.count(N) ? D[N] : -1;
  if (res >= Inf) res = -1;
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(12) << fixed;
  cin >> N >> M;
  for (int i = 0; i < (M); ++i) {
    cin >> L[i] >> R[i] >> W[i];
  }
  solve();
  return 0;
}
