#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M, inf = 1001001001;
  cin >> N >> M;
  vector<vector<pair<int, int>>> G(N + 1, vector<pair<int, int>>(0));
  for (int i = 2; i <= N; i++) G[i].push_back(make_pair(i - 1, 0));
  for (int i = 0; i < M; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    G[l].push_back(make_pair(r, c));
  }
  vector<int> D(N + 1, inf);
  vector<bool> state(N + 1, false);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      Q;
  D[1] = 0;
  Q.push(make_pair(0, 1));
  while (!Q.empty()) {
    pair<int, int> x = Q.top();
    Q.pop();
    int c = x.first;
    int u = x.second;
    state[u] = true;
    if (D[u] < c) continue;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i].first;
      if (state[v] == false && D[u] + G[u][i].second < D[v]) {
        D[v] = D[u] + G[u][i].second;
        Q.push(make_pair(D[v], v));
      }
    }
  }
  if (D[N] == inf)
    cout << -1 << endl;
  else
    cout << D[N] << endl;
  return 0;
}
