#include <bits/stdc++.h>
using namespace std;
pair<pair<long long, long long>, long long> p[100000];
const long long INF = 1e+15;
vector<int> G[100000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    p[i] = pair<pair<long long, long long>, long long>(
        pair<long long, long long>(l, r), c);
  }
  sort(p, p + M);
  map<int, long long> mp;
  mp[0] = 0;
  mp[N - 1] = INF;
  for (int i = 0; i < M; i++) {
    int l = p[i].first.first;
    int r = p[i].first.second;
    long long c = p[i].second;
    auto ptr = mp.lower_bound(l);
    if (mp.count(r) == 0) {
      mp[r] = ptr->second + c;
    } else {
      mp[r] = min(ptr->second + c, mp[r]);
    }
  }
  if (mp[N - 1] >= INF) {
    cout << -1 << endl;
  } else {
    cout << mp[N - 1] << endl;
  }
}
