#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
#define in(v) v; cin >> v;
void ins() {}
template<class T,class... Rest>void ins(T& v,Rest&... rest){cin>>v;ins(rest...);}
#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))

struct Range {
  int l, r, c;
};

#include <functional>
template<class T, class Lazy> struct LazySegTree {
  private:
  public:
    int size;
    vector<T> v; vector<Lazy> lazy;
    vector<int> seg_size;
    using F = function<T(T,T)>;
    using G = function<Lazy(Lazy,Lazy)>;
    using H = function<T(T,Lazy,int)>;
    T default_value; Lazy default_lazy;
    F f; G g; H h;
  public:
    LazySegTree(int n, T default_value, Lazy default_lazy, F f, G g, H h)
      : default_value(default_value), default_lazy(default_lazy), f(f), g(g), h(h) {
        for (size = 1; size < n; size *= 2);

        v.resize(2 * size - 1, default_value);
        lazy.resize(4 * size - 1, default_lazy);
        seg_size.resize(2 * size - 1, 1);

        for (int i = 0; i < size - 1; ++i) {
          int j = size - 2 - i;
          seg_size[j] = seg_size[2 * j + 1] + seg_size[2 * j + 2];
        }
      }
    void update(int index, T val) {
      update(index, index+1, default_lazy);
      index = index + size - 1;
      v[index] = val;
      while (index > 0) {
        index = (index - 1) / 2;
        v[index] = f(v[2 * index + 1], v[2 * index + 2]);
      }
    }
    void eval(int index) {
      v[index] = h(v[index], lazy[index], seg_size[index]);

      lazy[2 * index + 1] = g(lazy[index], lazy[2 * index + 1]);
      lazy[2 * index + 2] = g(lazy[index], lazy[2 * index + 2]);

      lazy[index] = default_lazy;
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      eval(index);
      if (cur_r <= l || r <= cur_l) return default_value;
      if (l <= cur_l && cur_r <= r) return v[index];

      int mid = (cur_l + cur_r) / 2;
      return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2));
    }
    void update(int l, int r, Lazy x) { update(l, r, x, 0, size, 0); }
    void update(int l, int r, Lazy x, int cur_l, int cur_r, int index) {
      eval(index);
      if (cur_r <= l || r <= cur_l) return;
      if (l <= cur_l && cur_r <= r) {
        lazy[index] = x;
        eval(index);
        return;
      }
      int mid = (cur_l + cur_r) / 2;
      update(l, r, x, cur_l, mid, 2 * index + 1);
      update(l, r, x, mid, cur_r, 2 * index + 2);
      v[index] = f(v[2 * index + 1], v[2 * index + 2]);
    }
};
const ll INF = 1e18;

int main() {
  int in(N); int in(M);

  // chmin
  LazySegTree<ll, ll> T(N, INF, INF,
      [](ll a, ll b) {return min(a, b);},
      [](ll a, ll b) { return a == INF ? b : min(a, b);},
      [](ll a, ll lz, int _) {return lz == INF ? a : min(a, lz);});
  T.update(0, 1, 0);

  vector<Range> range(M);
  rep(i, M) {
    int l, r, c;
    ins(l, r, c);
    --l, --r;
    range[i].l = l;
    range[i].r = r;
    range[i].c = c;
  }

  auto sorter = [&](const Range& lhs, const Range& rhs) {
    if (lhs.l == rhs.l) return lhs.r < rhs.r;
    return lhs.l < rhs.l;
  };

  all(sort, range, sorter);
  rep(i, M) {
    T.update(range[i].l, range[i].r+1, T.query(range[i].l, range[i].r+1) + range[i].c);
  }
  ll ans = T.query(N-1, N);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Measured memory_bytes: 8576