#include <bits/stdc++.h>
using namespace std;
const long long int maxd = 0x3f3f3f3f3f3f3f3f;
int main(int argc, char *argv[]) {
  vector<pair<int, pair<int, long long int>>> vp;
  int n, mm;
  cin >> n >> mm;
  for (int i = 0; i < mm; i++) {
    long long int l, r, c;
    cin >> l >> r >> c;
    vp.push_back(make_pair(l, make_pair(r, c)));
  }
  sort(vp.begin(), vp.end());
  map<int, long long int> m;
  m[1] = 0;
  for (int i = 0; i < vp.size(); i++) {
    long long int mind = -1;
    int start = vp[i].first, end = vp[i].second.first;
    if (start == end) continue;
    long long int d = vp[i].second.second;
    auto it1 = m.lower_bound(start);
    auto it2 = m.upper_bound(end);
    if (it1 == it2) {
      cout << -1 << endl;
      return 0;
    }
    mind = it1->second + d;
    it2--;
    if (mind < it2->second) {
      it2++;
      m.erase(it1, it2);
    }
    if (m.find(end) == m.end())
      m[end] = mind;
    else
      m[end] = min(m[end], mind);
  }
  if (m.find(n) == m.end()) m[n] = -1;
  cout << m[n] << endl;
}
