#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

struct RUP {
  int n;
  vector<int> dat, laz;
  const int def = 1e9 + 7;
  RUP() {}
  RUP(int n_) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, def);
    laz.clear();
    laz.resize(2 * n - 1, -1);
  }
  inline void eval(int len, int k) {
    len++;
    len--;
    if (laz[k] < 0) return;
    if (k * 2 + 1 < n * 2 - 1) {
      laz[k * 2 + 1] = laz[k];
      laz[k * 2 + 2] = laz[k];
    }
    dat[k] = laz[k];
    laz[k] = -1;
  }
  int update(int a, int b, int x, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l) return dat[k];
    if (a <= l && r <= b) return laz[k] = x;
    eval(r - l, k);
    dat[k] = min(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                 update(a, b, x, k * 2 + 2, (l + r) / 2, r));
    return dat[k];
  }
  int query(int a, int b, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l) return def;
    if (a <= l && r <= b) return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  void update(int a, int b, int x) { update(a, b, x, 0, 0, n); }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};

int main() {

  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> v;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    v.emplace_back(l - 1, r - 1, c);
  }
  sort(v.begin(), v.end(),
       [](auto &lhs, auto &rhs) { return get<1>(lhs) < get<1>(rhs); });
  RUP t(n);
  const int inf = 1e9 + 7;
  t.update(0, 1, 0);
  for (auto p : v) {
    int l, r, c;
    tie(l, r, c) = p;
    auto mn = t.query(l, r);
    // cout << l << " " << r << " " << c << " " << mn << endl;
    auto cur = t.query(r, r + 1);
    if (mn + c < cur) t.update(r, r + 1, mn + c);
  }
  // rep(i, n) { cout << t.query(i, i + 1) << " "; }
  auto ans = t.query(n - 1, n);
  if (ans == inf) ans = -1;
  cout << ans << endl;

  return 0;
}
