#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long> > ed;
  for (long long i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    ed.push_back(make_pair(make_pair(l, r), c));
  }
  set<pair<long long, long long> > st;
  st.insert(make_pair(1, 0));
  st.insert(make_pair(n + 1, 1000000000000007LL));
  sort(ed.begin(), ed.end());
  for (long long i = 0; i < m; i++) {
    long long lef = ed[i].first.first;
    long long rig = ed[i].first.second;
    long long cost = ed[i].second;
    auto itr = st.lower_bound(make_pair(lef, -1));
    pair<long long, long long> P = *itr;
    itr++;
    while ((*itr).second <= rig) {
      pair<long long, long long> Q = *itr;
      if (Q.first <= rig && Q.second >= P.second + cost) {
        itr = st.erase(itr);
      } else {
        itr++;
      }
    }
    itr = st.lower_bound(make_pair(rig, -1));
    if ((*itr).second > P.second + cost) {
      st.insert(make_pair(rig, P.second + cost));
    }
  }
  auto itr = st.lower_bound(make_pair(n, -1));
  pair<long long, long long> ans = *itr;
  if (ans.second != 1000000000000007LL) {
    cout << ans.second << endl;
    return 0;
  } else {
    cout << -1 << endl;
    return 0;
  }
  return 0;
}
