#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
template <typename T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (T& x : vec) is >> x;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  os << "{";
  for (long long i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, map<T, U>& map_var) {
  os << "{";
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& set_var) {
  os << "{";
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
void dump_func() { cerr << endl; }
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&&... tail) {
  cerr << head;
  if (sizeof...(Tail) > 0) {
    cerr << ", ";
  }
  dump_func(std::move(tail)...);
}
struct RUP {
  long long n;
  vector<long long> dat, laz;
  const long long def = INT_MAX;
  RUP() {}
  RUP(long long n_) { init(n_); }
  void init(long long n_) {
    n = 1;
    while (n < n_) n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, def);
    laz.clear();
    laz.resize(2 * n - 1, -1);
  }
  inline void eval(long long len, long long k) {
    if (laz[k] < 0) return;
    if (k * 2 + 1 < n * 2 - 1) {
      laz[k * 2 + 1] = laz[k];
      laz[k * 2 + 2] = laz[k];
    }
    dat[k] = laz[k];
    laz[k] = -1;
  }
  long long update(long long a, long long b, long long x, long long k,
                   long long l, long long r) {
    eval(r - l, k);
    if (r <= a || b <= l) return dat[k];
    if (a <= l && r <= b) return laz[k] = x;
    eval(r - l, k);
    dat[k] = min(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                 update(a, b, x, k * 2 + 2, (l + r) / 2, r));
    return dat[k];
  }
  long long query(long long a, long long b, long long k, long long l,
                  long long r) {
    eval(r - l, k);
    if (r <= a || b <= l) return def;
    if (a <= l && r <= b) return dat[k];
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  void update(long long a, long long b, long long x) {
    update(a, b, x, 0, 0, n);
  }
  long long query(long long a, long long b) { return query(a, b, 0, 0, n); }
};
signed main() {
  long long N, M;
  cin >> N >> M;
  vector<tuple<long long, long long, long long>> X(M);
  vector<long long> dist(123456, 0);
  for (long long i = 0, i_len = (M); i < i_len; ++i) {
    cin >> get<0>(X[i]) >> get<1>(X[i]) >> get<2>(X[i]);
    get<0>(X[i]);
    get<1>(X[i]);
  }
  sort(X.begin(), X.end());
  cerr << "----" << endl;
  for (long long i = 0, i_len = (M); i < i_len; ++i) {
    cerr << get<0>(X[i]) << " " << get<1>(X[i]) << " " << get<2>(X[i]) << endl;
  }
  RUP rup(N + 1);
  rup.update(1, 2, 0);
  for (long long i = 0, i_len = (M); i < i_len; ++i) {
    long long prev_val = rup.query(get<0>(X[i]), get<0>(X[i]) + 1);
    auto it = lower_bound(rup.dat.begin() + rup.dat.size() / 2 + 1,
                          rup.dat.end(), prev_val) -
              rup.dat.begin();
    rup.update(it + 1, get<1>(X[i]) + 1, prev_val + get<2>(X[i]));
    ;
    for (long long j = 1; j <= N; j++) cerr << rup.query(j, j + 1) << " ";
    cerr << endl;
  }
  if (rup.query(N - 1, N) == INT_MAX)
    cout << -1 << endl;
  else
    cout << rup.query(N - 1, N) << endl;
  return 0;
}
