#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
using VL = V<long long>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<long long, long long>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<long long> v);
template <typename T>
void drop(T a);
void solve() {
  long long N, M;
  cin >> N >> M;
  V<P<PL, long long>> edges(M);
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    edges[i] = {pair<long long, long long>(r, l), c};
  }
  sort(edges.begin(), edges.end());
  long long INF = 1e18;
  V<long long> dp(N, INF);
  dp[N - 1] = 0;
  long long next_edge_i = M - 1;
  for (long long i = N - 1; i >= 0; i--) {
    if (dp[i] == 1e18) continue;
    while (next_edge_i >= 0) {
      auto edge = edges[next_edge_i];
      if (i > edge.first.first) break;
      next_edge_i--;
      long long l = edge.first.second;
      long long c = edge.second;
      chmin(dp[l], dp[i] + c);
    }
  }
  long long res = dp[0] == INF ? -1 : dp[0];
  cout << res << endl;
}
struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  try {
    solve();
  } catch (exit_exception& e) {
  }
  return 0;
}
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print_ints(vector<long long> v) {
  for (long long i = 0; i < (v.size()); ++i) {
    if (i > 0) {
      cout << " ";
    }
    cout << v[i];
  }
  cout << endl;
}
template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}
