#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N, M;
vector<tuple<ll, ll, ll>> E;
typedef pair<ll, ll> P;

ll solve() {
  ll v = 0, d = 0, e = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  while (v != N - 1) {
    for (; e < E.size() && get<0>(E[e]) <= v; ++e) {
      ll l, r, c;
      tie(l, r, c) = E[e];
      que.push(make_pair(d + c, r));
    }
    if (que.empty()) return -1;
    ll dx, r;
    tie(dx, r) = que.top();
    que.pop();
    d = dx;
    v = r;
  }
  return d;
}
int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    E.push_back(make_tuple(l, r, c));
  }
  sort(E.begin(), E.end());
  cout << solve() << endl;
  return 0;
}
