#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<tuple<long long, long long, long long>> lrc(m);
  for (int i = 0; i < m; i++) {
    long long l;
    cin >> l;
    long long r;
    cin >> r;
    long long c;
    cin >> c;
    get<0>(lrc.at(i)) = l - 1;
    get<1>(lrc.at(i)) = r - 1;
    get<2>(lrc.at(i)) = c;
  }
  sort(lrc.begin(), lrc.end());
  map<long long, long long> mp;
  long long val = 0;
  while (get<0>(lrc.at(val)) == 0) {
    if (mp.count(get<1>(lrc.at(val)))) {
      mp[get<1>(lrc.at(val))] =
          min(mp.at(get<1>(lrc.at(val))), get<2>(lrc.at(val)));
    } else {
      mp[get<1>(lrc.at(val))] = get<2>(lrc.at(val));
    }
    val++;
  }
  while (val < m) {
    for (auto it = mp.begin(); it != mp.end(); it++) {
      long long a = it->first;
      if ((get<0>(lrc.at(val)) <= it->first) &&
          (get<1>(lrc.at(val)) > it->first)) {
        if (mp.count(get<1>(lrc.at(val)))) {
          mp[get<1>(lrc.at(val))] = min(mp.at(get<1>(lrc.at(val))),
                                        get<2>(lrc.at(val)) + (it->second));
        } else {
          mp[get<1>(lrc.at(val))] = get<2>(lrc.at(val)) + (it->second);
        }
      }
    }
    val++;
  }
  if (mp.count(n - 1)) {
    cout << mp.at(n - 1) << endl;
  } else {
    cout << -1 << endl;
  }
}
