#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvvll = vector<vector<vector<vector<ll>>>>;
using pl4 = pair<ll, ll>;
using str = string;
using vpl4 = vector<pair<ll, ll>>;
ll MOD = 998244353;
ll INF = 1000000001;
vector<ll> value;
ll N;
void update(ll i, ll x) {
  i += N - 1;
  value[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    value[i] = min(value[i * 2 + 1], value[i * 2 + 2]);
  }
}
ll query(ll a, ll b, ll k, ll l, ll r) {
  if (r <= a || b <= l) return INF;
  if (a <= l && r <= b)
    return value[k];
  else {
    ll c1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll c2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(c1, c2);
  }
}
signed main() {
  ll(n);
  cin >> (n);
  ;
  ll(m);
  cin >> (m);
  ;
  vector<pair<pair<ll, ll>, ll>> lrc(m);
  for (ll i = 0; i <= m - 1; i++) {
    cin >> lrc[i].first.first >> lrc[i].first.second >> lrc[i].second;
  }
  sort(lrc.begin(), lrc.end());
  N = 1;
  while (N < n) N *= 2;
  value = vector<ll>(2 * N - 1, INF);
  ll u = 0;
  update(0, 0);
  for (ll i = 0; i <= m - 1; i++) {
    u = query(lrc[i].first.first - 1, lrc[i].first.second, 0, 0, N);
    if (value[lrc[i].first.second + N - 2] > u + lrc[i].second) {
      update(lrc[i].first.second - 1, u + lrc[i].second);
    }
  }
  cout << ((value[n + N - 2] == INF ? -1 : value[n + N - 2])) << endl;
}
