#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T>
using VT = vector<T>;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const int MAX = 1e5 + 10;
int N, M;
int L[MAX], R[MAX];
LL C[MAX];
VT<pair<LL, int>> _edges[MAX];
map<int, VT<pair<LL, int>>> edges;
int indexes[MAX];
LL dist[MAX];
int main(void) {
  cin >> N >> M;
  for (int(i) = 0; (i) < (M); (i)++) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    _edges[L[i]].emplace_back(C[i], R[i]);
  }
  for (int(i) = 0; (i) < (N); (i)++) {
    if (_edges[i].empty()) continue;
    sort(begin(((_edges[i]))), end(((_edges[i]))));
    edges[i] = _edges[i];
  }
  edges[N - 1] = VT<pair<LL, int>>();
  fill(dist, dist + MAX, LLINF);
  priority_queue<pair<LL, int>, VT<pair<LL, int>>, greater<pair<LL, int>>> pq;
  pq.emplace(0, 0);
  dist[0] = 0;
  while (!pq.empty()) {
    const auto cur = pq.top();
    pq.pop();
    if (dist[cur.second] < cur.first) continue;
    const auto &cur_edges = edges[cur.second];
    auto nxt = edges.upper_bound(cur.second);
    auto itr = begin(cur_edges);
    while (nxt != end(edges) && itr != end(cur_edges)) {
      while (itr != end(cur_edges) && itr->second <= nxt->first) {
        itr++;
      }
      if (itr == end(cur_edges)) break;
      const auto nxt_cost = cur.first + itr->first;
      if (nxt_cost < dist[nxt->first]) {
        dist[nxt->first] = nxt_cost;
        pq.emplace(nxt_cost, nxt->first);
      }
      nxt++;
    }
  }
  cout << (dist[N - 1] == LLINF ? -1 : dist[N - 1]) << endl;
}
