#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M;
  cin >> N >> M;
  vector<vector<vector<ll>>> path(N);
  for (ll i = 0; i < M; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    path.at(l - 1).push_back({r - 1, c});
  }
  multiset<ll> st;//min
  vector<vector<ll>> imos(N + 1);
  st.insert(0);
  imos.at(1).push_back(0);
  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < imos.at(i).size(); ++j) {
      st.erase(st.lower_bound(imos.at(i).at(j)));
    }
    if (st.size() == 0) {
      cout << "-1" << "\n";
      return 0;
    }
    ll m = *st.begin();
    for (ll j = 0; j < path.at(i).size(); ++j) {
      ll cost = m + path.at(i).at(j).at(1);
      st.insert(cost);
      imos.at(path.at(i).at(j).at(0) + 1).push_back(cost);
    }
  }
  // for (ll i = 0; i < N + 1; ++i) {
  //   cout << "i" << i << "\n";
  //   for (ll j = 0; j < imos.at(i).size(); ++j) {
  //     cout << imos.at(i).at(j) << " ";
  //   }
  //   cout << "\n";
  // }
  cout << *st.begin() << "\n";
}
