#include <bits/stdc++.h>
using namespace std;
typedef struct E {
  long long to, cost;
} edge;
const long long MAX_V = 200100;
const long long Inf = 1e18;
vector<vector<edge>> E(MAX_V);
long long d[MAX_V];
void dijkstra(long long s) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  fill_n(d, MAX_V, Inf);
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) continue;
    for (int j = 0; j < E[v].size(); j++) {
      edge t = E[v][j];
      if (d[t.to] > d[v] + t.cost) {
        d[t.to] = d[v] + t.cost;
        que.push(pair<long long, long long>(d[t.to], t.to));
      }
    }
  }
}
long long nxt[222222];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) nxt[i] = 1e18;
  for (int i = 0; i < m; i++) {
    long long f, t, c;
    cin >> f >> t >> c;
    E[--f].push_back({--t, c});
    for (int j = f; j < t; j++) {
      if (nxt[j] == 1e18) {
        nxt[j] = t;
        E[j + 1].push_back({j, 0});
      } else {
        j = nxt[j] - 1;
      }
    }
  }
  dijkstra(0);
  if (d[n - 1] == Inf)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
  return 0;
}
