#include <bits/stdc++.h>
using namespace std;
const long long INF = 1145141919810364364;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<long long>> paths(m, vector<long long>(3));
  priority_queue<vector<long long>> que;
  vector<long long> minCost(n + 1, INF);
  minCost[1] = 0;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    paths[i] = {l, r, c};
  }
  sort(paths.begin(), paths.end());
  int pos = 0;
  for (int i = 1; i <= n; i++) {
    while (pos < m && paths[pos][0] == i) {
      que.push({paths[pos][1], paths[pos][2]});
      pos++;
    }
    vector<vector<long long>> trash;
    int minx = 1145141919;
    while (!que.empty()) {
      if (que.top()[0] < i) break;
      vector<long long> path = que.top();
      que.pop();
      if (minx > path[1]) {
        trash.push_back(path);
        minx = path[1];
        minCost[path[0]] = min(minCost[path[0]], minCost[i] + path[1]);
      } else
        break;
    }
    que.empty();
    for (auto x : trash) que.push(x);
  }
  cout << (minCost[n] == INF ? -1 : minCost[n]) << endl;
  return 0;
}
