#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, long long>, int> > c;
map<int, long long> a;
const long long INF = 1e14;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int L, R, C;
    cin >> L >> R >> C;
    c.push_back(pair<pair<int, long long>, int>(pair<int, long long>(R, L), C));
  }
  a.insert(make_pair(1, 0));
  sort(c.begin(), c.end());
  for (int i = 0; i < c.size(); i++) {
    auto itr = a.lower_bound(c[i].first.second);
    if (itr == a.end()) {
      continue;
    }
    if (a.find(c[i].first.first) == a.end()) {
      a.insert(make_pair(c[i].first.first, itr->second + c[i].second));
    } else {
      long long tmp = min(a[c[i].first.first], itr->second + c[i].second);
      a[c[i].first.first] = tmp;
    }
  }
  if (a.find(n) != a.end()) {
    cout << a[n] << endl;
  } else
    cout << -1 << endl;
}
