#include <bits/stdc++.h>
using namespace std;
struct input {
  long long L, R, C;
  bool operator<(const input& another) const { return L < another.L; };
};
template <typename T>
struct SegmentTree {
 private:
  long long n;
  vector<T> node;
  T init_v;

 public:
  SegmentTree(vector<T> a, T _init_v) {
    long long sz = a.size();
    n = 1;
    init_v = _init_v;
    while (n < sz) n *= 2;
    node.resize(2 * n, init_v);
    for (long long i = 0; i < sz; i++) node[i + n - 1] = a[i];
    for (long long i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(long long pos, T v) {
    long long k = pos + n - 1;
    node[k] = v;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = min(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  T get(long long pos) {
    long long k = pos + n - 1;
    return node[k];
  }
  T query(long long a, long long b, long long k = 0, long long l = 0,
          long long r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return init_v;
    if (a <= l && r <= b) return node[k];
    T lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(lv, rv);
  }
};
int main() {
  long long N, M;
  cin >> N >> M;
  vector<input> v;
  for (long long i = 0; i < M; i++) {
    input IN;
    cin >> IN.L >> IN.R >> IN.C;
    v.push_back(IN);
  }
  sort(v.begin(), v.end());
  vector<long long> _v;
  for (long long i = 0; i <= N; i++) {
    _v.push_back(1000000002);
  }
  SegmentTree<long long> ST(_v, 1000000002);
  ST.update(1, 0);
  for (auto itr = v.begin(); itr != v.end(); itr++) {
    long long s = ST.query(itr->L, itr->R);
    s += itr->C;
    s = min(ST.get(itr->R), s);
    ST.update(itr->R, s);
  }
  long long j = ST.get(N);
  if (j == 1000000002) {
    cout << -1 << endl;
  } else {
    cout << j << endl;
  }
  return 0;
}
