#include <bits/stdc++.h>
using namespace std;
template <typename T, typename E>
struct SegmentTree {
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  int n;
  F f;
  G g;
  T d1;
  vector<T> dat;
  SegmentTree(int n_, F f, G g, T d1, vector<T> v = vector<T>()) {
    this->f = f;
    this->g = g;
    this->d1 = d1;
    init(n_);
    if (n_ == (int)v.size()) build(n_, v);
  }
  void init(int n_) {
    n = 1;
    while (n < n_) n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, d1);
  }
  void build(int n_, vector<T> v) {
    for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  void update(int k, E a) {
    k += n - 1;
    dat[k] = g(dat[k], a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return d1;
    if (a <= l && r <= b) return dat[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
};
int main() {
  int n, m;
  cin >> n >> m;
  int INF = 2 * 1e9;
  vector<array<int, 3>> Q(m);
  SegmentTree<int, int> rmq(
      n, [](int a, int b) { return min(a, b); }, [](int a, int b) { return b; },
      INF);
  for (int i = 0; i < m; i++) {
    cin >> Q[i][0] >> Q[i][1] >> Q[i][2];
  }
  sort(Q.begin(), Q.end());
  rmq.update(0, 0);
  for (int i = 0; i < m; i++) {
    int l = Q[i][0] - 1, r = Q[i][1] - 1, c = Q[i][2];
    int a = rmq.query(r, n);
    int s = rmq.query(l, n);
    rmq.update(r, min(a, s + c));
  }
  int ans = rmq.query(n - 1, n);
  cout << (ans == INF ? -1 : ans) << endl;
}
