#include <bits/stdc++.h>
using namespace std;
const long long e5 = 1 << 20;
const long long mods[] = {998244353, 1000000007, 1000000009};
const long long bases[] = {997, 1009, 1013};
const long long e3 = 1 << 13;
const long long INF = 1ll << 50;
template <class T>
class segment_tree {
 private:
  long long n;
  T d;
  vector<T>* v;
  T _query(long long a, long long b, long long k, long long l, long long r) {
    if (r <= a || b <= l) return d;
    if (a <= l && r <= b)
      return this->v->at(k);
    else {
      T vector<long long> = _query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
      return func(vector<long long>, vr);
    }
  }

 public:
  segment_tree() {}
  segment_tree(long long n, T d = INT_MAX) {
    this->n = 1;
    while (n > this->n) this->n <<= 1;
    v = new vector<T>(this->n * 2, d);
    this->d = d;
  }
  void update(long long i, T x) {
    i += n - 1;
    this->v->at(i) = x;
    while (i > 0) {
      i = (i - 1) / 2;
      this->v->at(i) = func(this->v->at(i * 2 + 1), this->v->at(i * 2 + 2));
    }
  }
  T query(long long a, long long b) { return _query(a, b, 0, 0, n); }
  T func(T a, T b) { return min(a, b); }
};
long long n, m;
vector<vector<long long> > lrc;
int main() {
  cin >> n >> m;
  segment_tree<long long> sg(n + 1);
  sg.update(1, 0);
  lrc = vector<vector<long long> >(m, vector<long long>(3));
  for (long long i = 0; i < m; i++) {
    cin >> lrc[i][0] >> lrc[i][1] >> lrc[i][2];
  }
  sort(lrc.begin(), lrc.end());
  for (long long i = 0; i < m; i++) {
    long long c = sg.query(lrc[i][0], lrc[i][1] + 1);
    long long x = sg.query(lrc[i][1], lrc[i][1] + 1);
    sg.update(lrc[i][1], min(x, c + lrc[i][2]));
  }
  if (sg.query(n, n + 1) == INT_MAX)
    cout << -1 << endl;
  else
    cout << sg.query(n, n + 1) << endl;
}
