#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);
struct s_t {
  ll l, r, c;
};
bool asc(s_t left, s_t right) {
  return left.l != right.l
             ? left.l < right.l
             : (left.c != right.c ? left.c < right.c : left.r < right.r);
}
struct RMQRUQ {
  ll n;
  vector<ll> dat, lazy;
  RMQRUQ() {}
  RMQRUQ(ll n_) {
    n = 1;
    while (n < n_) n *= 2;
    dat.assign(n * 2, LLONG_MAX);
    lazy.assign(n * 2, LLONG_MAX);
  }
  void eval(ll len, ll k) {
    if (lazy[k] == LLONG_MAX) return;
    if (k * 2 + 1 < n * 2 - 1) {
      lazy[2 * k + 1] = min(lazy[k], lazy[2 * k + 1]);
      lazy[2 * k + 2] = min(lazy[k], lazy[2 * k + 2]);
    }
    dat[k] = min(dat[k], lazy[k]);
    lazy[k] = LLONG_MAX;
  }
  ll update(ll a, ll b, ll x, ll k, ll l, ll r) {
    eval(r - l, k);
    if (b <= l || r <= a) return dat[k];
    if (a <= l && r <= b) {
      lazy[k] = min(lazy[k], x);
      return lazy[k];
    }
    return dat[k] = min(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
                        update(a, b, x, 2 * k + 2, (l + r) / 2, r));
  }
  ll update(ll a, ll b, ll x) { return update(a, b, x, 0, 0, n); }
  ll query(ll a, ll b, ll k, ll l, ll r) {
    eval(r - l, k);
    if (b <= l || r <= a) return LLONG_MAX;
    if (a <= l && r <= b) return dat[k];
    ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  ll query(ll a, ll b) { return query(a, b, 0, 0, n); }
};
int main() {
  ll n, m, ans = 0;
  ll f = 0;
  cin >> n >> m;
  s_t z;
  vector<s_t> v;
  for (ll i = 0; i < m; i++) {
    cin >> z.l >> z.r >> z.c;
    z.l--;
    z.r--;
    v.push_back(z);
  }
  sort(v.begin(), v.end(), asc);
  if (v[0].l != 0) {
    cout << -1 << endl;
    return 0;
  }
  RMQRUQ s(n);
  s.update(0, 1, 0);
  s.update(v[0].l + 1, v[0].r + 1, v[0].c);
  for (ll i = 1; i < m; i++) {
    ll x = s.query(v[i].l, v[i].r + 1);
    if (x == LLONG_MAX) f = 1;
    s.update(v[i].l, v[i].r + 1, x + v[i].c);
  }
  ans = s.query(n - 1, n);
  if (f == 0 && ans != LLONG_MAX)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
