#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 7;
const int INF = (int)1e9 + 17;
const int mod = 998244353;
int n, m;
long long d[N];
vector<pair<int, int>> g[N];
multiset<long long> dist;
set<pair<int, long long>> life_time;
long long getmin(int p) {
  while (!life_time.empty() && life_time.begin()->first < p) {
    dist.erase(dist.find(life_time.begin()->second));
    life_time.erase(life_time.begin());
  }
  if (dist.empty()) {
    cout << -1 << endl;
    exit(0);
  }
  return *dist.begin();
}
void update(int l, int r, long long dist) {
  life_time.insert({r, dist});
  ::dist.insert(dist);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cin >> n >> m;
  vector<int> ls = {1, n};
  for (int i = 0; i < m; ++i) {
    int l, r, x;
    cin >> l >> r >> x;
    g[l].emplace_back(r, x);
    ls.emplace_back(l);
  }
  sort(ls.begin(), ls.end());
  ls.erase(unique(ls.begin(), ls.end()), ls.end());
  for (int v : ls) {
    d[v] = (v == 1 ? 0 : getmin(v));
    for (const auto& e : g[v]) {
      update(v, e.first, d[v] + e.second);
    }
  }
  cout << d[n] << endl;
  return 0;
}
