#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

struct Edge {
  int l;
  int r;
  int64 c;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  vector<vector<Edge>> v_to_edges(n);
  for (int i = 0; i < m; i++) {
    int l;
    int r;
    int64 c;
    cin >> l >> r >> c;
    l--;
    r--;
    Edge e({l, r, c});
    v_to_edges[l].push_back(e);
  }

  multiset<int64> s;
  constexpr int64 INF = 1E18;
  vector<int64> dp(n, INF);
  vector<vector<int64>> v_to_ends(n);
  dp[0] = 0;
  for (const auto& e : v_to_edges[0]) {
    s.insert(e.c);
    v_to_ends[e.r].push_back(e.c);
  }

  for (int v = 1; v < n; v++) {
    if (!s.empty()) {
      dp[v] = *s.begin();
    }
    if (dp[v] == INF) continue;
    for (int64 x : v_to_ends[v]) {
      s.erase(s.find(x));
    }
    for (const auto& e : v_to_edges[v]) {
      s.insert(dp[v] + e.c);
      v_to_ends[e.r].push_back(dp[v] + e.c);
    }
  }

  if (dp[n - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[n - 1] << endl;
  }
}