#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++) {
    if (vp[i].first != 1) m[vp[i].first] = -1;
    if (vp[i].second.first != 1) m[vp[i].second.first] = -1;
  }
  m[n] = -1;
  for (int i = 0; i < vp.size(); i++) {
    long long int mind = -1;
    int start = vp[i].first, end = vp[i].second.first;
    long long int d = vp[i].second.second;
    auto it1 = m.lower_bound(start);
    auto it2 = m.upper_bound(end);
    for (auto it = it1; it != it2; it++) {
      if (mind == -1)
        mind = it->second;
      else if (it->second != -1)
        mind = min(mind, it->second);
    }
    if (mind != -1) mind += d;
    for (auto it = it1; it != it2 && mind != -1; it++) {
      if (m[it->first] == -1)
        m[it->first] = mind;
      else
        m[it->first] = min(m[it->first], mind);
    }
  }
  cout << m[n] << endl;
}
