#include <bits/stdc++.h>
using namespace std;
namespace LST_ {
using RET = long long;
constexpr int BUF = 1048576 + 20;
RET t[BUF];
int ptr = 0;
inline RET* get(const int size) {
  ptr += size;
  return t + ptr - size;
}
}  // namespace LST_
struct LazySegTree {
  using T = LST_::RET;
  T* maxi;
  T* lazy;
  static const T NIL = 1e9 + 8;
  static const int N = 262144;
  LazySegTree() {
    maxi = LST_::get(2 * N);
    lazy = LST_::get(2 * N);
    for (int i = 0; i < (N + 10); i++) {
      lazy[i] = NIL;
      maxi[i] = 1e16;
    }
  }
  void setLazy(int k, T v) {
    lazy[k] = v;
    maxi[k] = v;
  }
  void push(int k) {
    if (lazy[k] == NIL) {
      return;
    }
    setLazy(k * 2 + 0, lazy[k]);
    setLazy(k * 2 + 1, lazy[k]);
    lazy[k] = NIL;
  }
  T compar(T a, T b) { return min(a, b); }
  void fix(int k) { maxi[k] = compar(maxi[k * 2], maxi[k * 2 + 1]); }
  void fill(int queryL, int queryR, T val, int k = 1, int nodeL = 0,
            int nodeR = N) {
    if (nodeR <= queryL || queryR <= nodeL) {
      return;
    }
    if (queryL <= nodeL && nodeR <= queryR) {
      setLazy(k, val);
      return;
    }
    push(k);
    int nodeM = (nodeL + nodeR) / 2;
    fill(queryL, queryR, val, k * 2 + 0, nodeL, nodeM);
    fill(queryL, queryR, val, k * 2 + 1, nodeM, nodeR);
    fix(k);
  }
  T maximum(int queryL, int queryR, int k = 1, int nodeL = 0, int nodeR = N) {
    if (nodeR <= queryL || queryR <= nodeL) {
      return (1e16);
    }
    if (queryL <= nodeL && nodeR <= queryR) {
      return maxi[k];
    }
    push(k);
    int nodeM = (nodeL + nodeR) / 2;
    T vl = maximum(queryL, queryR, k * 2 + 0, nodeL, nodeM);
    T vr = maximum(queryL, queryR, k * 2 + 1, nodeM, nodeR);
    return compar(vl, vr);
  }
};
int N, M;
int L[100001];
int R[100001];
int C[100001];
vector<pair<int, int> > v;
int main() {
  cin >> N >> M;
  for (int i = 0; i < (M); i++) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    R[i]--;
    v.push_back(pair<int, int>(L[i], i));
  }
  sort(v.begin(), v.end());
  LazySegTree seg;
  seg.fill(0, 1, 0);
  for (int i = 0; i < (v.size()); i++) {
    int x = v[i].second;
    long long now = seg.maximum(L[x], 100010);
    long long now2 = seg.maximum(R[x], 100010);
    now2 = min(now2, now + C[x]);
    seg.fill(R[x], R[x] + 1, now2);
  }
  long long ans = seg.maximum(N - 1, 100010);
  if (ans > 1e15) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
