// -----------------------------------
// Author     : MatsuTaku
// Affiliation: Tokushima University
// Country    : Japan
// Date       : 03/24/2020
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, typename E, typename Query, typename Eval, typename Propagate>
class lazy_segment_tree {
 private:
  Query query_;
  Eval eval_;
  Propagate prop_;

  size_t size_;
  T invalid_val_;
  E default_eval_;
  std::vector<T> tree_;
  std::vector<E> lazy_;

 public:
  lazy_segment_tree(size_t size, T invalid_val, E default_eval) {
    size_ = 1;
    while (size_ < size)
      size_ *= 2;
    invalid_val_ = invalid_val;
    default_eval_ = default_eval;
    tree_.resize(size_*2, invalid_val_);
    lazy_.resize(size_*2, default_eval_);
  }

  template <typename Iter>
  lazy_segment_tree(Iter begin, Iter end, T invalid_val, E default_eval) : lazy_segment_tree(end-begin, invalid_val, default_eval) {
    for (auto it = begin; it != end; ++it) {
      tree_[size_ + it - begin] = *it;
    }
    for (size_t i = size_-1; i > 0; i--) {
      tree_[i] = query_(tree_[i*2], tree_[i*2+1]);
    }
  }

  T operator[](size_t index) {
    return query(index, index+1);
  }

  void set(size_t index, T val) {
    auto i = size_ + index;
    tree_[i] = val;
    i /= 2;
    while (i > 0) {
      tree_[i] = query_(tree_[i*2], tree_[i*2+1]);
      i /= 2;
    }
  }

  void range_update(size_t l, size_t r, E e) {
    _range_update(l, r, e, 1, 0, size_);
  }

  T query(size_t l, size_t r) {
    return _query(l, r, 1, 0, size_);
  }

  void propagate(size_t d, size_t l, size_t r) {
    auto e = lazy_[d];
    tree_[d] = eval_(tree_[d], e, l, r);
    if (d < size_) {
      lazy_[d*2] = prop_(lazy_[d*2], e);
      lazy_[d*2+1] = prop_(lazy_[d*2+1], e);
    }
    lazy_[d] = default_eval_;
  }

 private:
  T _query(size_t l, size_t r, size_t _d, size_t _l, size_t _r) {
    if (r <= _l or _r <= l) {
      return invalid_val_;
    }
    propagate(_d, _l, _r);
    if (l <= _l and _r <= r) {
      return tree_[_d];
    } else {
      auto c = (_l+_r)/2;
      return query_(_query(l, r, _d*2 , _l, c),
                    _query(l, r,_d*2+1, c ,_r));
    }
  }

  void _range_update(size_t l, size_t r, E e, size_t _d, size_t _l, size_t _r) {
    propagate(_d, _l, _r);
    if (r <= _l or _r <= l) {
      return;
    } else if (l <= _l and _r <= r) {
      lazy_[_d] = prop_(lazy_[_d], e);
      propagate(_d, _l, _r);
    } else {
      auto c = (_l+_r)/2;
      _range_update(l, r, e, _d * 2, _l, c);
      _range_update(l, r, e, _d * 2 + 1, c, _r);
      tree_[_d] = query_(tree_[_d*2], tree_[_d*2+1]);
    }
  }

};

struct query {
  ll operator()(ll a, ll b) {return min(a,b);}
};

struct eval {
  ll operator()(ll a, ll b, ll, ll) {return min(a,b);}
};

constexpr ll INF = 1e18;

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

  int N,M; cin>>N>>M;
  struct Query {int l,r,c; Query()=default;Query(int l,int r,int c) : l(l),r(r),c(c){}};
  vector<Query> qs(M);
  for (int i = 0; i < M; i++) {
    int l,r,c; cin>>l>>r>>c;
    qs[i] = {l-1,r-1,c};
  }
  sort(qs.begin(), qs.end(), [](auto l, auto r) {return l.l<r.l;});

  using min_segment_tree = lazy_segment_tree<ll, ll, query, eval, query>;
  min_segment_tree rmq(N, INF, INF);
  rmq.set(0, 0);
  for (auto q : qs) {
    rmq.range_update(q.l, q.r+1, rmq[q.l]+q.c);
  }
  auto ans = rmq[N-1];

  cout << (ans==INF?-1:ans) << endl;

  return 0;
}


Measured memory_bytes: 5504