#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, pair<int, int>>> v[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    v[x].push_back({-c, {x, y}});
  }
  priority_queue<pair<int, pair<int, int>>> pq;
  for (auto g : v[1]) {
    pq.push(g);
  }
  vector<long long> ans(n + 1, 1000000000000000000);
  ans[1] = 0;
  int p = 2;
  while (!pq.empty()) {
    int cost = -pq.top().first;
    int l = pq.top().second.first;
    int r = pq.top().second.second;
    pq.pop();
    while (p <= r) {
      ans[p] = ans[l] + cost;
      for (auto g : v[p]) {
        pq.push(g);
      }
      p++;
    }
  }
  if (ans[n] != 1000000000000000000)
    cout << ans[n];
  else
    cout << -1;
}
