#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long mod = 1e9 + 7;
const int inf = 2e9;
const long long lnf = 9e18;
template <typename X, typename M, typename T>
struct SegTreeLazy {
  using FX = function<X(X, X)>;
  using FA = function<X(X, M)>;
  using FM = function<M(M, M)>;
  int n;
  FX fx;
  FA fa;
  FM fm;
  const X ex;
  const M em;
  vector<X> dat;
  vector<M> lazy;
  SegTreeLazy(int n_, FX fx_, FA fa_, FM fm_, X ex_, M em_)
      : n(),
        fx(fx_),
        fa(fa_),
        fm(fm_),
        ex(ex_),
        em(em_),
        dat(n_ * 4, ex),
        lazy(n_ * 4, em) {
    int x = 1;
    while (n_ > x) x *= 2;
    n = x;
  }
  void set(int i, X x) { dat[i + n - 1] = x; }
  void build() {
    for (int k = n - 2; k >= 0; k--)
      dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
  }
  void eval(int k) {
    if (lazy[k] == em) return;
    if (k < n - 1) {
      lazy[k * 2 + 1] = fm(lazy[k * 2 + 1], lazy[k]);
      lazy[k * 2 + 2] = fm(lazy[k * 2 + 2], lazy[k]);
    }
    dat[k] = fa(dat[k], lazy[k]);
    lazy[k] = em;
  }
  void update(int a, int b, M x, int k, int l, int r) {
    eval(k);
    if (a <= l && r <= b) {
      lazy[k] = fm(lazy[k], x);
      eval(k);
    } else if (a < r && l < b) {
      update(a, b, x, k * 2 + 1, l, (l + r) / 2);
      update(a, b, x, k * 2 + 2, (l + r) / 2, r);
      dat[k] = fx(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  void update(int a, int b, M x) { update(a, b, x, 0, 0, n); }
  X query_sub(int a, int b, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l) {
      return ex;
    } else if (a <= l && r <= b) {
      return dat[k];
    } else {
      X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return fx(vl, vr);
    }
  }
  X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
  X find_rightest(int a, int b, T x) {
    return find_rightest_sub(a, b, x, 0, 0, n);
  }
  X find_leftest(int a, int b, T x) {
    return find_leftest_sub(a, b, x, 0, 0, n);
  }
  X find_rightest_sub(int a, int b, T x, int k, int l, int r) {
    eval(k);
    if (dat[k] > x || r <= a || b <= l) {
      return a - 1;
    } else if (k >= n - 1) {
      return (k - (n - 1));
    } else {
      X vr = find_rightest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
      if (vr != a - 1) {
        return vr;
      } else {
        return find_rightest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
      }
    }
  }
  X find_leftest_sub(int a, int b, T x, int k, int l, int r) {
    eval(k);
    if (dat[k] > x || r <= a || b <= l) {
      return b;
    } else if (k >= n - 1) {
      return (k - (n - 1));
    } else {
      X vl = find_leftest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
      if (vl != b) {
        return vl;
      } else {
        return find_leftest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
      }
    }
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  using X = int;
  using M = int;
  auto fx = [](X x1, X x2) -> X { return min(x1, x2); };
  auto fa = [](X x, M m) -> X { return min(x, m); };
  auto fm = [](M m1, M m2) -> M { return min(m1, m2); };
  int ex = numeric_limits<int>::max();
  int em = numeric_limits<int>::max();
  SegTreeLazy<X, M, int> rmq(n, fx, fa, fm, ex, em);
  rmq.update(0, 1, 0);
  vector<tuple<int, int, int> > inputt(m);
  for (int i = 0; i < (int)(m); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    inputt[i] = tuple<int, int, int>(l, r, c);
  }
  sort((inputt).begin(), (inputt).end());
  for (int i = 0; i < (int)(m); i++) {
    int l, r, c;
    tie(l, r, c) = inputt[i];
    int mn = rmq.query(l, r + 1);
    if (mn == numeric_limits<int>::max()) continue;
    rmq.update(l, r + 1, mn + c);
  }
  int ans = rmq.query(n - 1, n);
  if (ans != numeric_limits<int>::max())
    cout << rmq.query(n - 1, n) << endl;
  else
    cout << -1 << endl;
}
