#include <bits/stdc++.h>
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
template <class T>
struct SegmentTree {
  using F = function<T(T, T)>;
  const T init;
  const F f;
  int sz;
  vector<T> seg;
  SegmentTree(int n, const T init, const F f) : init(init), f(f) {
    sz = 1;
    while (sz < n) sz <<= 1;
    seg.resize(sz << 1, init);
  }
  void update(int id) {
    while (id >>= 1) {
      seg[id] = f(seg[2 * id], seg[2 * id + 1]);
    }
  }
  void replace(int id, T x) {
    id += sz;
    seg[id] = x;
    update(id);
  }
  T get(int l, int r) {
    T L = init, R = init;
    for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = f(L, seg[l++]);
      if (r & 1) R = f(seg[--r], R);
    }
    return f(L, R);
  }
  void debug() {
    int i = 1;
    for (int r = 2; r <= 2 * sz; r <<= 1) {
      while (i < r) {
        cout << seg[i++] << " ";
      }
      cout << "\n";
    }
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  SegmentTree<int64_t> seg(n, LINF,
                           [](int64_t l, int64_t r) { return min(l, r); });
  seg.replace(0, 0);
  tuple<int, int, int> p[m];
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    p[i] = make_tuple(l, r, c);
  }
  sort(p, p + m);
  priority_queue<tuple<int, int, int64_t>, vector<tuple<int, int, int64_t>>,
                 greater<tuple<int, int, int64_t>>>
      pq;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    tie(l, r, c) = p[i];
    int64_t cost = seg.get(l, r + 1);
    if (cost != LINF) {
      pq.emplace(l, -r, c);
      continue;
    } else {
      i--;
      if (pq.size() == 0) {
        cout << -1 << endl;
        return 0;
      }
      while (pq.size()) {
        int64_t ll, rr, cc;
        tie(ll, rr, cc) = pq.top();
        rr = -rr;
        pq.pop();
        int64_t ncos = seg.get(ll, rr + 1) + cc;
        if (ncos <= seg.get(rr, rr + 1)) {
          seg.replace(rr, ncos);
        }
      }
    }
  }
  while (pq.size()) {
    int64_t ll, rr, cc;
    tie(ll, rr, cc) = pq.top();
    rr = -rr;
    pq.pop();
    int64_t ncos = seg.get(ll, rr + 1) + cc;
    if (ncos <= seg.get(rr, rr + 1)) {
      seg.replace(rr, ncos);
    }
  }
  int64_t ans = seg.get(n - 1, n);
  cout << (ans == LINF ? -1 : ans) << endl;
  return 0;
}
