#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()
ll MOD = 1000000007;
const ll INF = 1LL << 60;

class LazySegTree {
 public:
  const ll def_val_ = INF;
  vl val_, lazy_;
  ll n_;

  ll comp(ll l, ll r) { return min(l, r); }

  ll getr(ll x, ll y, ll k, ll l, ll r) {
    if (y <= l || r <= x) return def_val_;

    // lazy evaluation
    eval(k, l, r);

    if (x <= l && r <= y) return val_[k];
    auto vl = getr(x, y, 2 * k + 1, l, (l + r) / 2);
    auto vr = getr(x, y, 2 * k + 2, (l + r) / 2, r);
    return comp(vl, vr);
  }

  void eval(ll k, ll l, ll r) {
    if (lazy_[k] != def_val_) {
      // val_[k] += lazy_[k];
      val_[k] = min(val_[k], lazy_[k]);
      if (r - l > 1) {
        // has children
        lazy_[2 * k + 1] = min(lazy_[2 * k + 1], lazy_[k]);
        lazy_[2 * k + 2] = min(lazy_[2 * k + 1], lazy_[k]);
      }
      lazy_[k] = def_val_;
    }
  }

  void add(ll x, ll y, ll v, ll k, ll l, ll r) {
    eval(k, l, r);
    if (y <= l || r <= x) return;
    if (x <= l && r <= y) {
      lazy_[k] = min(lazy_[k], v);
      eval(k, l, r);
    } else {
      add(x, y, v, 2 * k + 1, l, (l + r) / 2);
      add(x, y, v, 2 * k + 2, (l + r) / 2, r);
      val_[k] = comp(val_[2 * k + 1], val_[2 * k + 2]);
    }
  }

 public:
  LazySegTree(ll size) {
    n_ = 1;
    while (n_ < size) n_ *= 2;
    val_ = vl(2 * n_, def_val_);
    lazy_ = vl(2 * n_, def_val_);
  }

  void update(ll i, ll v) {
    i += (n_ - 1);
    val_[i] = v;
    while (i > 0) {
      i = (i - 1) / 2;
      val_[i] = comp(val_[2 * i + 1], val_[2 * i + 2]);
    }
  }

  void addRange(ll x, ll y, ll v) { add(x, y, v, 0, 0, n_); }

  // get XXX value between [x,y)
  ll getRange(ll x, ll y) { return getr(x, y, 0, 0, n_); }
};

bool cmp(const tuple<ll, ll, ll>& a, const tuple<ll, ll, ll>& b) {
  if (get<0>(a) < get<0>(b))
    return true;
  else if (get<0>(a) == get<0>(b) && get<1>(a) < get<1>(b))
    return true;
  return false;
}

int main() {
  //
  ll n, m;
  cin >> n >> m;
  LazySegTree st(n);
  st.update(0, 0);
  vector<tuple<ll, ll, ll>> intervals;
  rep(i, m) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    intervals.push_back({l, r, c});
  }
  sort(ALL(intervals), cmp);
  rep(i, m) {
    tuple<ll, ll, ll> iv = intervals[i];
    ll l, r, c;
    l = get<0>(iv);
    r = get<1>(iv);
    c = get<2>(iv);
    // cerr << l << "," << r << "," << c << endl;
    ll mind = st.getRange(l, r + 1);
    st.addRange(l, r + 1, mind + c);
    // st.getRange(0, n);
    // rep(j, n) st.getRange(j, j + 1);
    // rep(j, n) { cerr << st.getRange(j, j + 1) << " "; }
    // cerr << endl;
  }
  ll ans = st.getRange(n - 1, n);
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 8564