#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
double EPS = 1e-8;
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
  out << '(' << a.first << ", " << a.second << ')';
  return out;
}
}  // namespace std
template <typename T>
T chmax(T& a, const T& b) {
  return a = (a > b ? a : b);
}
template <typename T>
T chmin(T& a, const T& b) {
  return a = (a < b ? a : b);
}
template <typename T>
struct SegTree {
  int n;
  vector<T> dat, lazy;
  const T NIL = 2e18;
  const T DFLT;
  SegTree(int n_, const T& initV, const T& dflt) : DFLT(dflt) {
    n = 1;
    while (n < n_) n *= 2;
    dat.assign(2 * n - 1, initV);
    lazy.assign(2 * n - 1, NIL);
  }
  void add(int l, int r, const T& t) { add(l, r, t, 0, 0, n); }
  T query(int l, int r) { return query(l, r, 0, 0, n); }
  int lower_bound(int l, int r, T v) {
    if (query(r - 1, r) < v) return -1;
    if (query(l, l + 1) >= v) return l;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (query(m, m + 1) < v)
        l = m;
      else
        r = m;
    }
    return r;
  }

 private:
  T f(const T& a, const T& b) { return min(a, b); }
  void fix(int k) { dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]); }
  void setLazy(int k, const T& v) {
    lazy[k] = v;
    dat[k] = v;
  }
  void push(int k) {
    if (lazy[k] == NIL) return;
    setLazy(k * 2 + 1, lazy[k]);
    setLazy(k * 2 + 2, lazy[k]);
    lazy[k] = NIL;
  }
  void add(int queryL, int queryR, const T& val, int k, int nodeL, int nodeR) {
    if (nodeR <= queryL || queryR <= nodeL) {
      return;
    }
    if (queryL <= nodeL && nodeR <= queryR) {
      setLazy(k, val);
      return;
    }
    push(k);
    int nodeM = (nodeL + nodeR) / 2;
    add(queryL, queryR, val, k * 2 + 1, nodeL, nodeM);
    add(queryL, queryR, val, k * 2 + 2, nodeM, nodeR);
    fix(k);
  }
  T query(int queryL, int queryR, int k, int nodeL, int nodeR) {
    if (nodeR <= queryL || queryR <= nodeL) {
      return DFLT;
    }
    if (queryL <= nodeL && nodeR <= queryR) {
      return dat[k];
    }
    push(k);
    int nodeM = (nodeL + nodeR) / 2;
    T vector<long long> = query(queryL, queryR, k * 2 + 1, nodeL, nodeM);
    T vr = query(queryL, queryR, k * 2 + 2, nodeM, nodeR);
    return f(vector<long long>, vr);
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int>> a(m);
  for (int i = (0); i < (int)(m); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    --l;
    a[i] = {{l, r}, c};
  }
  sort(a.begin(), a.end());
  SegTree<long long> st(n + 10, 1e18, 1e18);
  st.add(0, 1, 0);
  for (int i = (0); i < (int)(m); i++) {
    int l = a[i].first.first, r = a[i].first.second;
    long long c = a[i].second;
    long long t = st.query(l, r);
    auto m = st.lower_bound(l, r, c + t);
    if (m == -1) continue;
    if (st.query(m, r) <= c + t) assert(false);
    st.add(m, r, c + t);
  }
  long long ans = st.query(n - 1, n);
  cout << (ans == 1e18 ? -1 : ans) << endl;
  return 0;
}
