#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
long long dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
long long dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T1, typename T2>
inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<tuple<long long, long long, long long>> edges(m);
  for (long long i = 0; i < (m); i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    edges[i] = tie(l, r, c);
  }
  sort((edges).begin(), (edges).end());
  long long now = 0;
  vector<long long> v(n, inf);
  set<long long> se;
  se.insert(0);
  v[0] = 0;
  for (long long i = 0; i < (m); i++) {
    long long l, r, c;
    tie(l, r, c) = edges[i];
    l--;
    r--;
    auto itrl = se.lower_bound(l);
    v[r] = min(v[r], v[*itrl] + c);
    auto itr = se.lower_bound(r);
    if (itr == se.end())
      se.insert(r);
    else {
      itr--;
      if (v[*itr] >= v[r]) se.erase(itr);
      itr++;
      if (v[r] < v[*itr]) se.insert(r);
    }
  }
  if (v[n - 1] != inf)
    cout << v[n - 1] << endl;
  else
    puts("-1");
}
