#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
template <typename Monoid, typename OperatorMonoid>
struct LazySegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  using pair<int, int> = function<OperatorMonoid(OperatorMonoid, int)>;
  int n;
  vector<Monoid> dat;
  vector<OperatorMonoid> laz;
  F f;
  G g;
  H h;
  pair<int, int> p;
  Monoid def;
  OperatorMonoid laz_def;
  LazySegmentTree(F f, G g, H h, pair<int, int> p, Monoid def,
                  OperatorMonoid laz_def)
      : f(f), g(g), h(h), p(p), def(def), laz_def(laz_def) {}
  void build(const vector<Monoid>& vec) {
    int siz = vec.size();
    n = 1;
    while (n < siz) n *= 2;
    dat.assign(2 * n - 1, def);
    laz.assign(2 * n - 1, laz_def);
    for (int i = 0; i < siz; i++) dat[n - 1 + i] = vec[i];
    for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]);
  }
  void update(int a, int b, OperatorMonoid x) { update(a, b, x, 0, 0, n); }
  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }

 private:
  void eval(int k, int l, int r) {
    if (laz[k] != laz_def) {
      dat[k] = g(dat[k], p(laz[k], r - l));
      if (r - l > 1) {
        laz[2 * k + 1] = h(laz[2 * k + 1], laz[k]);
        laz[2 * k + 2] = h(laz[2 * k + 2], laz[k]);
      }
      laz[k] = laz_def;
    }
  }
  void update(int a, int b, OperatorMonoid x, int k, int l, int r) {
    eval(k, l, r);
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      laz[k] = h(laz[k], x);
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }
  Monoid query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return def;
    eval(k, l, r);
    if (a <= l && r <= b) return dat[k];
    Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
};
signed main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, long long>> vec;
  vector<pair<int, int>> j;
  int l, r;
  long long c;
  for (int i = 0; i < (m); i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    vec.emplace_back(pair<int, int>(r, l), c);
    j.emplace_back(pair<int, int>(l, r));
  }
  sort((j).begin(), (j).end());
  int mx = 0;
  for (int i = 0; i < (m); i++) {
    if (j[i].first > mx) {
      cout << -1 << endl;
      return 0;
    }
    mx = max(mx, j[i].second);
  }
  sort((vec).begin(), (vec).end());
  auto f = [](int a, int b) { return min(a, b); };
  auto g = [](int a, int b) { return min(a, b); };
  auto h = [](int a, int b) { return min(a, b); };
  auto p = [](int a, int b) { return a; };
  LazySegmentTree<long long, long long> seg(f, g, h, p, 1e14, 1e14);
  seg.build(vector<long long>(n, 1e14));
  seg.update(0, 1, 0);
  for (int i = 0; i < (m); i++) {
    int l = vec[i].first.second, r = vec[i].first.first;
    long long t = seg.query(l, r + 1);
    seg.update(l, r + 1, t + vec[i].second);
  }
  long long t = seg.query(n - 1, n);
  cout << t << endl;
  return 0;
}
