#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
int N;
int M;
signed main() {
  using Event = pair<pair<long long, long long>, long long>;
  priority_queue<Event, vector<Event>, greater<Event>> evs;
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N >> M;
  for (int(i) = 0; (i) < (M); ++(i)) {
    long long l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    evs.push(Event(pair<long long, long long>(l, c), r));
  }
  const long long INF = 1LL << 61;
  set<pair<long long, long long>> costs;
  costs.insert(pair<long long, long long>(0, 0));
  costs.insert(pair<long long, long long>(N + 1, INF));
  while (!evs.empty()) {
    auto e = evs.top();
    evs.pop();
    long long l, c, r;
    tie(l, c) = e.first;
    r = e.second;
    auto itr = costs.lower_bound(pair<long long, long long>(l, -1));
    auto itr2 = costs.lower_bound(pair<long long, long long>(r, -1));
    long long nc = itr->second + c;
    if (itr2->second > nc) {
      if (itr2->first == r) {
        costs.erase(itr2);
      }
      costs.insert(pair<long long, long long>(r, nc));
    }
  }
  auto itr = costs.lower_bound(pair<long long, long long>(N - 1, -1));
  if (itr->second == INF)
    cout << "-1" << endl;
  else
    cout << itr->second << endl;
}
