#include <bits/stdc++.h>
using namespace std;
void dykstra(int s, vector<long long>& dist,
             vector<vector<pair<long long, long long>>>& G) {
  priority_queue<pair<long long, long long>> q;
  dist[s] = 0;
  q.push(make_pair(0LL, static_cast<long long>(s)));
  pair<long long, long long> t;
  long long cur_idx;
  long long cur_dist;
  long long next_idx;
  long long next_dist;
  while (!q.empty()) {
    t = q.top();
    q.pop();
    cur_dist = t.first;
    cur_idx = t.second;
    if (dist[cur_idx] < cur_dist) continue;
    for (int i = 0; i < G[cur_idx].size(); ++i) {
      t = G[cur_idx][i];
      next_idx = t.first;
      next_dist = t.second;
      if (dist[next_idx] > cur_dist + next_dist) {
        dist[next_idx] = cur_dist + next_dist;
        q.push(make_pair(dist[next_idx], next_idx));
      }
    }
  }
}
void solve(long long N, long long M, std::vector<long long> L,
           std::vector<long long> R, std::vector<long long> C) {
  vector<vector<pair<long long, long long>>> G(
      N, vector<pair<long long, long long>>());
  for (int i = 0; i < M; i++) {
    G[L[i] - 1].emplace_back(R[i] - 1, C[i]);
  }
  for (int i = 0; i < N - 1; i++) {
    G[i + 1].emplace_back(i, 0);
  }
  vector<long long> dist(N, numeric_limits<long long>::max());
  dykstra(0, dist, G);
  if (dist[N - 1] == numeric_limits<long long>::max()) {
    cout << -1 << endl;
  } else {
    cout << dist[N - 1] << endl;
  }
}
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> L(M);
  std::vector<long long> R(M);
  std::vector<long long> C(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, M, std::move(L), std::move(R), std::move(C));
  return 0;
}
