#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
struct LazySegmentTree {
 private:
  ll n;
  vector<ll> node, lazy;
  vector<bool> lazyFlag;

 public:
  LazySegmentTree(vector<ll> v) {
    ll sz = (ll)v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, INF);
    lazyFlag.resize(2 * n - 1, false);
    for (ll i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (ll i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void lazyEvaluate(ll k, ll l, ll r) {
    if (lazyFlag[k]) {
      node[k] = min(node[k], lazy[k]);
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void update(ll a, ll b, ll x, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = min(lazy[k], x);
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return INF;
    if (a <= l && r <= b) return node[k];
    ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<pair<pair<ll, ll>, ll>> v(m);
  for (ll i = 0; i < m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    v[i] = {{l, r}, c};
  }
  sort(v.begin(), v.end());
  vector<ll> t(n, INF);
  LazySegmentTree tree(t);
  tree.update(0, 1, 0);
  for (ll i = 0; i < m; i++) {
    ll l = v[i].first.first, r = v[i].first.second, c = v[i].second;
    ll tmp = tree.find(l, l + 1);
    if (tmp == INF) continue;
    tree.update(l, r + 1, tmp + c);
  }
  ll ans = tree.find(n - 1, n);
  cout << ((ans != INF) ? ans : -1) << endl;
}
