#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long MOD = 998244353;
int inf = 1000001000;
long long INF = 100000000000000000;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename E>
struct SegmentTree {
  using H = function<E(E, E)>;
  long long n, height;
  H h;
  E ei;
  vector<E> laz;
  SegmentTree(H h, E ei) : h(h), ei(ei) {}
  void init(long long n_) {
    n = 1;
    height = 0;
    while (n < n_) n <<= 1, height++;
    laz.assign(2 * n, ei);
  }
  inline void propagate(long long k) {
    if (laz[k] == ei) return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    laz[k] = ei;
  }
  inline void thrust(long long k) {
    for (long long i = height; i; i--) propagate(k >> i);
  }
  void update(long long a, long long b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (long long l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) laz[l] = h(laz[l], x), l++;
      if (r & 1) --r, laz[r] = h(laz[r], x);
    }
  }
  E get_val(long long a) {
    thrust(a += n);
    return laz[a];
  }
  void set_val(long long a, E x) {
    thrust(a += n);
    laz[a] = x;
  }
};
int main() {
  long long n, m;
  cin >> n >> m;
  auto h = [&](long long x, long long y) { return min(x, y); };
  const long long INF = 1e18;
  SegmentTree<long long> seg(h, INF);
  seg.init(n);
  seg.set_val(0, 0);
  vector<long long> l(m), r(m), c(m);
  vector<vector<long long>> G(n + 1, vector<long long>(0));
  for (int(i) = 0; (i) < (m); (i)++) {
    cin >> l[i] >> r[i] >> c[i];
    G[--l[i]].push_back(i);
  }
  for (int(i) = 0; (i) < (n); (i)++) {
    long long d = seg.get_val(i);
    for (int(j) = 0; (j) < (G[i].size()); (j)++)
      seg.update(l[i], r[i], d + c[i]);
  }
  long long ans = seg.get_val(n - 1);
  if (ans >= INF) ans = -1;
  cout << ans << endl;
}
