#include <bits/stdc++.h>
using ll = long long;
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n, m;
  int ans = 0;
  cin >> n >> m;
  int l, r;
  ll c;
  vector<tuple<int, int, ll>> data(m);
  for (int i = 0, i_len = (int)(m); i < i_len; i++) {
    cin >> l >> r >> c;
    data[i] = make_tuple(l, r, c);
  }
  sort((data).begin(), (data).end());
  priority_queue<pair<int, ll>, vector<pair<int, ll>>, greater<pair<int, ll>>>
      q;
  q.push(make_pair(1, 0LL));
  pair<int, ll> now;
  for (int i = 0; i < m and !q.empty(); i++) {
    while (get<0>(data[i]) > q.top().first) {
      q.pop();
      if (q.empty()) {
        cout << -1 << endl;
        return 0;
      }
    }
    now = make_pair(get<1>(data[i]), q.top().second + get<2>(data[i]));
    while (q.top().second > now.second and q.top().first <= now.first and
           !q.empty()) {
      q.pop();
    }
    q.push(now);
  }
  while (q.top().first != n) {
    q.pop();
    if (q.empty()) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << q.top().second << endl;
}
