#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 55;
struct RangeMin {
 private:
  ll n;
  vector<ll> node;

 public:
  RangeMin(vector<ll> v) {
    size_t sz = v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1, INF);
    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[2 * i + 1], node[2 * i + 2]);
  }
  void update(ll x, ll val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[2 * x + 1], node[2 * x + 2]);
    }
  }
  ll getmin(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return node[k];
    ll vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
struct RangeMax {
 private:
  ll n;

 public:
  vector<ll> node;
  RangeMax(vector<ll> v) {
    size_t sz = v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1, INT_MIN);
    for (ll i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (ll i = n - 2; i >= 0; i--)
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
  }
  void update(ll x, ll val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = max(node[2 * x + 1], node[2 * x + 2]);
    }
  }
  ll getmax(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return INT_MIN;
    if (a <= l && r <= b) return node[k];
    ll vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
signed main() {
  ll N, M;
  cin >> N >> M;
  vector<tuple<ll, ll, ll>> ranges(M);
  for (ll i = 0; i < M; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    ranges[i] = make_tuple(l, r, c);
  }
  sort(ranges.begin(), ranges.end());
  RangeMin seg(vector<ll>(N, INF));
  vector<ll> dp(N, INF);
  dp[0] = 0;
  seg.update(0, 0);
  for (ll i = 0; i < M; i++) {
    ll l = get<0>(ranges[i]);
    ll r = get<1>(ranges[i]);
    ll c = get<2>(ranges[i]);
    dp[r] = min(dp[r], seg.getmin(l, r + 1) + c);
    seg.update(r, dp[r]);
  }
  if (dp[N - 1] >= INF) {
    cout << -1 << endl;
  } else {
    cout << dp[N - 1] << endl;
  }
  return 0;
}
