#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using iii = tuple<ll, ll, ll>;
using viii = vector<iii>;
using mii = map<ll, ll>;
ll INF = 2e18;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, l, r, c;
  cin >> n >> m;
  viii a(m);
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> c;
    a[i] = {l, r, c};
  }
  sort(a.begin(), a.end());
  mii x;
  x[1] = 0;
  x[2] = INF;
  for (int i = 0; i < m; i++) {
    tie(l, r, c) = a[i];
    auto it = x.upper_bound(l);
    it--;
    ll d = it->second + c;
    it = x.upper_bound(l);
    while (it != x.end()) {
      if (it->first > r) break;
      if (it->second > d) {
        ll p = it->second;
        x[it->first] = d;
        for (it++;; it++) {
          if (it == x.end()) {
            x[r + 1] = p;
            break;
          }
          if (it->first <= r)
            x.erase(it);
          else {
            if (it->first > r + 1) x[r + 1] = p;
            break;
          }
        }
        break;
      }
      it++;
    }
  }
  if (x.count(n) && x[n] != INF)
    cout << x[n] << "\n";
  else
    cout << "-1\n";
}
