#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> graph[n + 1];
  for (int l, r, c; cin >> l >> r >> c;) {
    graph[l].emplace_back(r, c);
  }
  for (int i = 1; i <= n; i++) {
    graph[i].emplace_back(i - 1, 0);
  }
  long long dist[n + 1];
  fill(dist, dist + n + 1, LLONG_MAX);
  dist[1] = 0;
  set<pair<int, int>> set;
  set.emplace(0, 1);
  do {
    auto current = set.begin()->second;
    set.erase(set.begin());
    for (auto& p : graph[current]) {
      if (dist[p.first] > dist[current] + p.second) {
        set.erase({dist[p.first], p.first});
        dist[p.first] = dist[current] + p.second;
        set.emplace(dist[p.first], p.first);
      }
    }
  } while (!set.empty());
  if (dist[n] == LLONG_MAX) {
    cout << -1;
  } else {
    cout << dist[n];
  }
  return 0;
}
