#include <bits/stdc++.h>
using namespace std;
using TUP = tuple<int, int, long long>;
const long long inf = 1e18;
int main() {
  int N, M;
  cin >> N >> M;
  int l, r;
  long long c;
  vector<TUP> tups(M);
  for (int i = 0; i < M; i++) {
    cin >> l >> r >> c;
    tups[i] = make_tuple(l, r, c);
  }
  sort(tups.begin(), tups.end());
  map<int, long long> dists;
  dists[1] = 0;
  for (auto t : tups) {
    tie(l, r, c) = t;
    if (dists.count(r) == 0) {
      dists[r] = inf;
    }
    auto it = dists.lower_bound(l);
    if (it == dists.end() || it->first > r) {
      continue;
    }
    dists[r] = min(dists[r], it->second + c);
  }
  long long ans = -1;
  if (dists.count(N) > 0 && dists[N] != inf) {
    ans = dists[N];
  }
  cout << ans << endl;
  return 0;
}
