#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];
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)++) {
    sort(begin(((edges[i]))), end(((edges[i]))));
  }
  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;
    int nxt = cur.second + 1;
    auto itr = begin(edges[cur.second]);
    while (nxt < N && itr != end(edges[cur.second])) {
      if (edges[nxt].empty() && nxt < N - 1) {
        nxt++;
        continue;
      }
      while (itr != end(edges[cur.second]) && itr->second <= nxt) {
        itr++;
      }
      if (itr == end(edges[cur.second])) break;
      const auto nxt_cost = cur.first + itr->first;
      if (nxt_cost < dist[nxt]) {
        dist[nxt] = nxt_cost;
        pq.emplace(nxt_cost, nxt);
      }
      nxt++;
    }
  }
  cout << (dist[N - 1] == LLINF ? -1 : dist[N - 1]) << endl;
}
