#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
#define chmax(x, y) do { x = max(x, y); } while(0)
#define chmin(x, y) do { x = min(x, y); } while(0)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, ) (__VA_ARGS__)  // NOLINT
template<typename T>
using reversed_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T> >;

struct T {
  ll l, r, c;
  T() {}
  T(ll l, ll r, ll c): l(l), r(r), c(c) {}
};

const ll INF = 1e17;
ll N, M;
vector<T> g;
vector<ll> ans;

signed main() {
  cin >> N >> M;
  g.resize(M);
  rep(i, M) {
    cin >> g[i].l >> g[i].r >> g[i].c;
    g[i].l--;
    g[i].r--;
  }
  sort(begin(g), end(g), [](const auto &x, const auto &y) { return x.l < y.l; });

  ans.resize(N, INF);
  ans[0] = 0;
  ll j = 0;
  rep(i, N) {
    if (ans[i] == INF)
      continue;
    while (j < N && g[j].l <= i) {
      chmin(ans[g[j].r], ans[i] + g[j].c);
      j++;
    }
  }

  if (ans[N-1] == INF)
    cout << -1 << endl;
  else
    cout << ans[N-1] << endl;
  return 0;
}
