#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;
struct SegmentTree {
  ll ide = 1000000000000000000;
  inline void updatef(ll &x, ll y) { x = min(x, y); }
  inline ll queryf(ll x, ll y) { return min(x, y); }
  int n;
  vector<ll> node;
  SegmentTree(int sz) {
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, ide);
  }
  ll get(int k) {
    ll ans = ide;
    k += n - 1;
    ans = min(ans, node[k]);
    while (k) {
      k = (k - 1) / 2;
      ans = min(ans, node[k]);
    }
  }
  void update(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      updatef(node[k], x);
      return;
    }
    update(a, b, x, 2 * k + 1, l, (l + r) / 2);
    update(a, b, x, 2 * k + 2, (l + r) / 2, r);
  }
};
struct edge {
  int l;
  int r;
  ll c;
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<edge> e(m);
  for (int i = 0; i < (m); i++) {
    cin >> e[i].l >> e[i].r >> e[i].c;
    e[i].l--, e[i].r;
  }
  sort((e).begin(), (e).end(), [](edge &l, edge &r) { return l.l < r.l; });
  SegmentTree seg(n);
  seg.update(0, 1, 0);
  for (int i = 0; i < (m); i++) {
    ll x = seg.get(e[i].l);
    seg.update(e[i].l, e[i].r, x + e[i].c);
  }
  ll ans = seg.get(n - 1);
  if (ans == 1000000000000000000)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
