#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;
const int INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<T> edges;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    edges.push_back(T(l, r, c));
  }
  sort((edges).begin(), (edges).end(), [](T a, T b) {
    int l1, r1, c1, l2, r2, c2;
    tie(l1, r1, c1) = a;
    tie(l2, r2, c2) = b;
    return l1 == l2 ? r1 < r2 : l1 < l2;
  });
  map<int, ll> dist;
  dist[0] = 0;
  for (auto e : edges) {
    int l, r, c;
    tie(l, r, c) = e;
    auto it = dist.lower_bound(l);
    if (it == dist.end()) {
      cout << -1 << endl;
      return 0;
    } else if (it->first < r) {
      ll rc = dist.find(r) == dist.end() ? LINF : dist[r];
      dist[r] = min(rc, it->second + c);
      vector<int> list;
      auto itr = dist.find(r);
      if (itr == dist.begin()) continue;
      --itr;
      for (auto it = itr; it->first >= l; --it) {
        if (it->second > dist[r]) {
          list.push_back(it->first);
        }
        if (it == dist.begin()) break;
      }
      for (auto v : list) {
        dist.erase(v);
      }
    }
  }
  if (dist.find(n - 1) == dist.end()) {
    cout << -1 << endl;
  } else {
    cout << dist[n - 1] << endl;
  }
  return 0;
}
