#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
template <typename T>
inline void chmin(T& a, T b) {
  if (a > b) a = b;
  return;
}
template <typename T>
inline void chmax(T& a, T b) {
  if (a < b) a = b;
  return;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  os << '{';
  for (size_t i = 0; i < vec.size(); ++i) {
    os << vec[i];
    if (i != vec.size() - 1) os << ',';
  }
  os << '}';
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  return os << '{' << p.first << " " << p.second << '}';
}
constexpr int64 INF = 1e18;
template <typename T>
class SegmentTree {
 public:
  SegmentTree(const std::vector<T>& vec, const T& initial_value,
              std::function<T(T, T)> operation)
      : initial_value_(initial_value), vec_(vec), operation_(operation) {
    Initialize();
  }
  void Update(int position, T new_value);
  T Query(int a, int b);
  T operator[](const int& position) const;

 private:
  int n_;
  const T initial_value_;
  const std::vector<T> vec_;
  std::vector<T> data_;
  std::function<T(T, T)> operation_;
  void Initialize();
};
template <typename T>
void SegmentTree<T>::Initialize() {
  int vsize = static_cast<int>(vec_.size());
  n_ = 1;
  while (n_ < vsize) {
    n_ <<= 1;
  }
  data_.assign(2 * n_, initial_value_);
  for (int i = 0; i < vsize; ++i) {
    data_[i + n_] = vec_[i];
  }
  for (int i = n_ - 1; i > 0; --i) {
    data_[i] = operation_(data_[2 * i], data_[2 * i + 1]);
  }
}
template <typename T>
void SegmentTree<T>::Update(int position, T new_value) {
  position += n_;
  data_[position] = new_value;
  while (position > 0) {
    position >>= 1;
    data_[position] = operation_(data_[2 * position], data_[2 * position + 1]);
  }
}
template <typename T>
T SegmentTree<T>::Query(int a, int b) {
  int vl = initial_value_, vr = initial_value_;
  for (a += n_, b += n_; a < b; a >>= 1, b >>= 1) {
    if (a & 1) vl = operation_(vl, data_[a++]);
    if (b & 1) vr = operation_(data_[--b], vr);
  }
  return operation_(vl, vr);
}
template <typename T>
T SegmentTree<T>::operator[](const int& position) const {
  return data_[n_ + position];
}
struct edge {
  int from, to;
  int64 cost;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<edge> v;
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    int64 cost;
    cin >> cost;
    v.push_back({l, r, cost});
  }
  sort(v.begin(), v.end(),
       [](const edge& x, const edge& y) { return x.to < y.to; });
  vector<int64> shortest_dis(n + 1, INF);
  shortest_dis[1] = 0;
  SegmentTree<int64> seg(shortest_dis, INF,
                         [](int64 x, int64 y) { return x < y; });
  for (int i = 0; i < m; ++i) {
    for (int j = v[i].from + 1; j <= v[i].to; ++j) {
      shortest_dis[j] =
          min(shortest_dis[j], shortest_dis[v[i].from] + v[i].cost);
    }
  }
  if (shortest_dis[n] == INF) {
    cout << -1 << endl;
  } else {
    cout << shortest_dis[n] << endl;
  }
  return 0;
}
