#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
void solve() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<pair<int, LL>> LC, RC;
  vector<vector<pair<int, LL>>> edges(100000);
  for (int i = 0; i < (m); i++) {
    int l, r;
    LL c;
    cin >> l >> r >> c;
    l--;
    r--;
    LC.push_back({l, c});
    RC.push_back({r, c});
    edges[l].push_back({r, c});
  }
  sort(LC.begin(), LC.end());
  sort(RC.begin(), RC.end());
  for (int i = 0; i < (LC.size() - 1); i++) {
    edges[LC[i + 1].first].push_back({LC[i].first, 0});
  }
  auto ritr = LC.rbegin();
  for (int i = RC.size() - 1; i > 0; i--) {
    while (ritr != LC.crend()) {
      if (ritr->first < RC[i].first) {
        edges[RC[i].first].push_back({ritr->first, 0});
        break;
      }
      ritr++;
    }
  }
  vector<LL> dp(100000);
  for (int i = 0; i < (n); i++) dp[i] = -1;
  priority_queue<pair<LL, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    auto top = q.top();
    q.pop();
    int p = top.second;
    LL d = -top.first;
    if (dp[p] != -1) continue;
    dp[p] = d;
    if (p == n - 1) break;
    for (int i = 0; i < (edges[p].size()); i++) {
      q.push({-(d + edges[p][i].second), edges[p][i].first});
    }
  }
  cout << dp[n - 1] << endl;
}
int main() {
  solve();
  return 0;
}
