#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<long long> ls(m), rs(m), cs(m);
  vector<pair<long long, long long>> lis;
  for (int i = 0; i < m; ++i) {
    cin >> ls[i] >> rs[i] >> cs[i];
    ls[i]--;
    rs[i]--;
    lis.emplace_back(ls[i], i);
  }
  sort(lis.begin(), lis.end());
  const long long inf = 1LL << 60;
  set<pair<long long, long long>> dist_lines;
  dist_lines.emplace(n - 1, inf);
  dist_lines.emplace(0, 0);
  for (int _i = 0; _i < m; ++_i) {
    const long long i = lis[_i].second;
    const long long l = ls[i];
    const long long r = rs[i];
    const long long c = cs[i];
    auto l_elem = *dist_lines.upper_bound(make_pair(l, -1));
    const long long new_cost = l_elem.second + c;
    dist_lines.emplace(r, new_cost);
  }
  long long ans = (*dist_lines.upper_bound(make_pair(n - 1, -1))).second;
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}
