#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
const int MOD = (int)1e9 + 7;
const long long INF = 1LL << 60;
const int inf = 1 << 30;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& V) {
  int N = V.size();
  for (int i = 0; i < (N); i++) {
    os << V[i];
    if (i != N - 1) os << " ";
  }
  os << "\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, pair<T, T> const& P) {
  os << P.first;
  os << " ";
  os << P.second;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& S) {
  auto it = S.begin();
  while (it != S.end()) {
    os << *it;
    os << " ";
    it++;
  }
  os << "\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T>& q) {
  for (auto it = q.begin(); it < q.end(); it++) {
    os << *it;
    os << " ";
  }
  os << endl;
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  vector<pair<pair<long long, long long>, long long>> LRC(M);
  long long l, r, c;
  for (int i = 0; i < (M); i++) {
    cin >> l >> r >> c;
    LRC[i] = make_pair(make_pair(l - 1, r - 1), c);
  }
  vector<long long> dp(N, INF);
  dp[0] = 0;
  set<long long> visited;
  visited.insert(0);
  sort((LRC).begin(), (LRC).end());
  for (int i = 0; i < (N); i++) {
    l = LRC[i].first.first;
    r = LRC[i].first.second;
    c = LRC[i].second;
    auto left = visited.lower_bound(l);
    if (visited.end() == left || *left >= r) {
      cout << -1 << endl;
      exit(0);
    }
    chmin(dp[r], dp[*left] + c);
    visited.insert(r);
  }
  if (dp[N - 1] >= INF) {
    cout << -1 << endl;
  } else
    cout << dp[N - 1] << endl;
}
