/**
 *      purpose : 
 *      author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *      created : 
**/

#include <iostream>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cctype>
#include <functional>
#include <iomanip>
#include <random>
#include <utility>

using namespace std;

using int64 = long long;
#define debug(_) cerr << #_ << ": " << (_) << '\n'
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]);
  }
}

// return Query[a, b)
template <typename T>
T SegmentTree<T>::Query(int a, int b) {
  T 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];
}

class edge {
 public:
  int from, to;
  int64 cost;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m; cin >> n >> m;
  vector<int64> dp(n, INF);
  dp[0] = 0;
  vector<edge> v;
  for (int i = 0; i < m; ++i) {
    int l, r; cin >> l >> r;
    int64 c; cin >> c;
    --l; --r;
    v.push_back((edge){l, r, c});
  }
  sort(v.begin(), v.end(), [](const edge& x, const edge& y) {
    return x.to < y.to;
  });
  SegmentTree<int64> seg(dp, INF, [](int64 x, int64 y) {
    return min(x, y);
  });

  for (int i = 0; i < m; ++i) {
    dp[v[i].to] = min(dp[v[i].to], seg.Query(v[i].from, v[i].to) + v[i].cost);
    seg.Update(v[i].to, dp[v[i].to]);
  }
  if (dp[n - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[n - 1] << endl;
  }

  return 0;
}

Measured memory_bytes: 5360