#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class Type>
struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream& operator<<(ostream& out, const vector<T>& v) {
  if (v.empty()) return out;
  constexpr bool is_vector_v = is_vector<T>::value;
  if (is_vector_v)
    for (auto itr = v.begin(); itr != v.end();)
      out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
  else
    for (auto itr = v.begin(); itr != v.end();)
      out << (*itr), out << ((++itr != v.end()) ? " " : "");
  return out;
}
inline void put() {}
template <class T>
inline void put(const T& first) {
  std::cout << first << "\n";
}
template <class T, class... N>
inline void put(const T& first, const N&... rest) {
  std::cout << first << " ";
  put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T& first, const N&... rest) {
  std::cout << first << "\n";
  putn(rest...);
}
int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<long long> l(m), r(m), c(m);
  using pi = pair<pair<long long, long long>, long long>;
  vector<pi> pp(m);
  for (long long i = 0; i < (long long)(m); i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i];
    pp[i].first.first = l[i];
    pp[i].first.second = r[i];
    pp[i].second = c[i];
  }
  sort((pp).begin(), (pp).end());
  for (long long i = 0; i < (long long)(m); i++) {
    l[i] = pp[i].first.first;
    r[i] = pp[i].first.second;
    c[i] = pp[i].second;
  }
  long long inf = LLONG_MAX / 3;
  vector<long long> dp(n, inf);
  priority_queue<pair<long long, long long>> pq;
  int cons = 1;
  int used = 1;
  for (long long i = 0; i < (long long)(m); i++) {
    if (l[i] == 0) {
      pq.push(pair<long long, long long>(-c[i], i));
      used = i + 1;
    }
  }
  dp[0] = 0;
  while (!pq.empty()) {
    pair<long long, long long> p = pq.top();
    pq.pop();
    if (dp[l[p.second]] != inf) {
      for (long long i = (long long)(cons); i < (long long)(r[p.second]); i++) {
        dp[i] = dp[l[p.second]] - p.first;
      }
      cons = r[p.second];
      while (used < m && l[used] <= cons) {
        pq.push(pair<long long, long long>(-c[used], used));
        used++;
      }
    }
  }
  if (dp[n - 1] == inf) {
    put(-1);
    return 0;
  }
  put(dp[n - 1]);
  return 0;
}
