#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int x, y, z;
  set<int> pos;
  map<int, vector<int>> rec;
  vector<int> l(m), r(m), d(m);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> d[i];
    pos.insert(l[i]);
    pos.insert(r[i]);
    if (!rec.count(l[i])) {
      rec[l[i]] = vector<int>();
    }
    rec[l[i]].push_back(i);
    if (!rec.count(r[i])) {
      rec[r[i]] = vector<int>();
    }
    rec[r[i]].push_back(i);
  }
  if (!rec.count(1)) {
    cout << -1 << endl;
    return 0;
  }
  set<pair<int, int>> sta;
  vector<int> f(m, -1);
  for (int i : rec[1]) {
    f[i] = d[i];
    sta.insert(make_pair(f[i], i));
  }
  pos.erase(1);
  for (int p : pos) {
    if (sta.size() == 0) {
      break;
    }
    for (int i : rec[p]) {
      if (l[i] == p) {
        f[i] = sta.begin()->first + d[i];
        sta.insert(make_pair(f[i], i));
      }
    }
    for (int i : rec[p]) {
      if (r[i] == p) {
        sta.erase(make_pair(f[i], i));
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < m; i++) {
    if (r[i] == n && f[i] != -1) {
      ans = ans == -1 ? f[i] : min(ans, f[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
