#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int n_max = 1e5 + 10;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long>>> graph(n);
  long long INF = INT_MAX;
  vector<long long> dp(n, INF);
  for (int i = 0; i < m; i++) {
    long long l, r;
    cin >> l >> r;
    long long c;
    cin >> c;
    l--;
    r--;
    graph[l].emplace_back(pair<long long, long long>(r, c));
  }
  dp[0] = 0;
  bool ok = true;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  for (int i = 0; i < n; i++) {
    if (graph[i].size() == 0) continue;
    if (dp[i] == INF) {
      pair<long long, long long> temp;
      if (pq.size() > 0)
        temp = pq.top();
      else {
        ok = false;
        break;
      }
      while (temp.second < i && pq.size() > 0) {
        pq.pop();
        temp = pq.top();
      }
      if (temp.second >= i)
        dp[i] = temp.first;
      else {
        ok = false;
        break;
      }
    } else {
      pair<long long, long long> temp = pair<long long, long long>(0, -1);
      if (pq.size() > 0) temp = pq.top();
      while (temp.second < i && pq.size() > 0) {
        pq.pop();
        temp = pq.top();
      }
      if (temp.second >= i) dp[i] = min(dp[i], temp.first);
    }
    for (auto j : graph[i]) {
      dp[j.first] = min(dp[j.first], dp[i] + j.second);
      pq.push(pair<long long, long long>(dp[j.first], j.first));
    }
  }
  cout << (ok ? dp[n - 1] : -1) << endl;
}
