#include <bits/stdc++.h>
using namespace std;
struct Pocz {
  int x, c;
};
struct Kon {
  int x, c;
};
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<int> l(m), p(m), c(m);
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> p[i] >> c[i];
    --l[i];
    --p[i];
  }
  vector<vector<Pocz> > pocz(n);
  vector<vector<Kon> > kon(n);
  for (int i = 0; i < m; ++i) {
    pocz[p[i]].push_back({l[i], c[i]});
    kon[l[i]].push_back({p[i], c[i]});
  }
  multiset<int> S;
  vector<int> odl(n, -1);
  odl[0] = 0;
  for (int i = 0; i < kon[0].size(); ++i) S.insert(kon[0][i].c);
  for (int i = 1; i < n; ++i) {
    if (S.size() != 0)
      odl[i] = *S.begin();
    else {
      cout << "-1";
      return 0;
    }
    for (int j = 0; j < pocz[i].size(); ++j)
      S.erase(S.find(odl[pocz[i][j].x] + pocz[i][j].c));
    for (int j = 0; j < kon[i].size(); ++j) S.insert(odl[i] + kon[i][j].c);
  }
  cout << odl[n - 1];
  return 0;
}
