#include <bits/stdc++.h>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using ll = long long;
using pint = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::vector;
int main() {
  int N, M;
  cin >> N >> M;
  vector<std::pair<pint, ll>> LRC(M);
  for (int i = (0), i_end_ = (M); i < i_end_; i++) {
    int l, r;
    ll c;
    cin >> l >> r;
    cin >> c;
    l--;
    r--;
    LRC[i] = std::make_pair(pint(l, r), c);
  }
  std::sort(LRC.begin(), LRC.end());
  vector<ll> d(N, 1e18);
  d[0] = 0;
  int reach = 0;
  for (int i = (0), i_end_ = (M); i < i_end_; i++) {
    int l = LRC[i].first.first;
    while (d[l] == 1e18) l++;
    int r = LRC[i].first.second;
    if (l < r && l <= reach) {
      d[r] = std::min(d[r], d[l] + LRC[i].second);
    }
    reach = r;
  }
  if (d[N - 1] == 1e18) {
    cout << -1 << endl;
  } else {
    cout << d[N - 1] << endl;
  }
  return 0;
}
