#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  map<int, map<int, long long> > m;
  for (int i = (0); i < (M); ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    m[R][L] = C;
  }
  map<int, long long> ans;
  ans[0] = 0;
  for (auto em : m) {
    ans[em.first] = 1e15;
    for (auto e : em.second) {
      auto p = ans.lower_bound(e.first);
      if (p == ans.end()) continue;
      ans[em.first] = min(ans[em.first], ans[p->first] + e.second);
    }
    auto itr1 = ans.lower_bound(em.first);
    long long v = itr1->second;
    auto itr2 = ans.lower_bound(em.first);
    itr2--;
    while (itr2->second >= v) {
      ans.erase(itr2);
      itr2 = ans.lower_bound(em.first);
      itr2--;
    }
  }
  if (ans[N - 1] == 0 or ans[N - 1] == 1e15) {
    cout << -1 << endl;
  } else {
    cout << ans[N - 1] << endl;
  }
  return 0;
}
