#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
template <typename Monoid>
struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  int sz;
  vector<Monoid> seg;
  const F f;
  const Monoid M1;
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }
  void set(int k, const Monoid &x) { seg[k + sz] = x; }
  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) L = f(L, seg[a++]);
      if (b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }
  Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
  int n, m;
  cin >> n >> m;
  long long l[100002], r[100002], c[100002];
  pair<pair<long long, long long>, int> pp[100002];
  SegmentTree<long long> seg(
      n, [](long long a, long long b) { return min(a, b); }, INF);
  for (int i = 0; i < n; i++) seg.set(i, INF);
  seg.build();
  seg.update(0, 0);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
    pp[i] = pair<pair<long long, long long>, int>(
        pair<long long, long long>(l[i], r[i]), c[i]);
  }
  sort(pp, pp + n);
  for (int i = 0; i < n; i++) {
    long long st = seg.query(pp[i].first.first, n);
    seg.update(pp[i].first.second,
               min(seg[pp[i].first.second], pp[i].second + st));
  }
  if (seg[n - 1] < INF)
    cout << seg[n - 1] << endl;
  else
    cout << -1 << endl;
}
