#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<pair<int, int>, ll>> LRC(M);
  for (int i = 0; i < M; i++) {
    cin >> LRC.at(i).first.first >> LRC.at(i).first.second >> LRC.at(i).second;
    LRC.at(i).first.first--;
    LRC.at(i).first.second--;
  }
  sort(LRC.begin(), LRC.end());
  priority_queue<int, vector<int>, greater<int>> pq;
  vector<ll> dist(N, 1000000000000000);
  for (int i = 0; i < M; i++) {
    if (LRC.at(i).first.first == 0) {
      pq.push(i);
    } else {
      break;
    }
  }
  dist.at(0) = 0;
  while (!pq.empty()) {
    int v = pq.top();
    pair<pair<int, int>, ll> p =
        make_pair(make_pair(LRC.at(v).first.second, N), 0);
    int k = upper_bound(LRC.begin(), LRC.end(), p) - LRC.begin();
    int C = LRC.at(v).second;
    for (int j = v + 1; j < k; j++) {
      int l = LRC.at(j).first.first;
      if (dist.at(l) > dist.at(v) + C) {
        dist.at(l) = dist.at(v) + C;
        pq.push(j);
      }
    }
    pq.pop();
  }
  ll ans = 1000000000000000;
  for (int i = 0; i < M; i++) {
    if (LRC.at(i).first.second == N - 1) {
      ans = min<ll>(ans, dist.at(LRC.at(i).first.first) + LRC.at(i).second);
    }
  }
  if (ans == 1000000000000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
