#include <bits/stdc++.h>
using namespace std;
const long long INF = (1 << 30) - 1;
const long long LLINF = (1LL << 60LL);
const long long MOD = 1000000007;
const long long mod = 998244353;
const long long MAX = 510000;
const double pi = acos(-1);
const double eps = 1e-9;
long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
static const long long MAX_N = 1 << 18;
pair<long long, long long> dat[2 * MAX_N - 1];
long long n;
long long dt[2 * MAX_N - 1];
struct SegmentTree {
  SegmentTree(long long n_) {
    n = 1;
    while (n < n_) n *= 2;
    for (long long i = 0; i < 2 * n - 1; i++) {
      dat[i].first = -1;
      dat[i].second = LLINF;
      dt[i] = LLINF;
    }
  }
  long long query(long long k) {
    k += n - 1;
    pair<long long, long long> value = dat[k];
    while (k > 0) {
      k = (k - 1) / 2;
      value = max(value, dat[k]);
    }
    return value.second;
  }
  long long query2(long long k) {
    k += n - 1;
    return k;
  }
  void update(long long a, long long b, long long k,
              pair<long long, long long> P, long long l, long long r) {
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      dat[k] = P;
      dt[k] = P.second;
    } else {
      update(a, b, k * 2 + 1, P, l, (l + r) / 2);
      update(a, b, k * 2 + 2, P, (l + r) / 2, r);
    }
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long _n, m;
  pair<pair<long long, long long>, long long> s[100010];
  cin >> _n >> m;
  SegmentTree sg(_n);
  for (long long i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    s[i] = make_pair(pair<long long, long long>(l, r), c);
  }
  sort(s, s + m);
  sg.update(0, 1, 0, pair<long long, long long>(0, 0), 0, n);
  for (long long i = 0; i < m; i++) {
    long long l = s[i].first.first, r = s[i].first.second, c = s[i].second;
    long long A = sg.query2(l);
    long long x = sg.query(l);
    long long s = lower_bound(dt + A, dt + A + (_n), x + c) - dt;
    sg.update(s - (n - 1), r + 1, 0, pair<long long, long long>(i + 1, x + c),
              0, n);
  }
  long long ans = sg.query(_n - 1);
  if (ans == LLINF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
