#include <bits/stdc++.h>
template <class Data, class Operator>
class SegmentTree {
  using DataMerger = std::function<Data(Data, Data)>;
  using Applier = std::function<Data(Operator, Data)>;

 private:
  int length;
  std::vector<Data> dat;
  Data dat_id;
  DataMerger dmerge;
  Applier app;
  Data query(int ql, int qr, int nidx, int nl, int nr) {
    if (nr <= ql || qr <= nl) return dat_id;
    if (ql <= nl && nr <= qr) return dat[nidx];
    Data vl = query(ql, qr, nidx * 2, nl, (nl + nr) / 2);
    Data vr = query(ql, qr, nidx * 2 + 1, (nl + nr) / 2, nr);
    return dmerge(vl, vr);
  }

 public:
  explicit SegmentTree(int N, Data dat_id, DataMerger dmerge, Applier app)
      : length(1), dat_id(dat_id), dmerge(dmerge), app(app) {
    while (length < N) length <<= 1;
    dat.assign(length * 2, dat_id);
  }
  Data query(int ql, int qr) { return query(ql, qr, 1, 0, length); }
  void update(int idx, Operator e) {
    int nidx = idx + length;
    dat[nidx] = app(e, dat[nidx]);
    while (nidx > 0) {
      nidx >>= 1;
      dat[nidx] = dmerge(dat[nidx * 2], dat[nidx * 2 + 1]);
    }
  }
};
using lint = long long;
constexpr lint INF = std::numeric_limits<lint>::max() / 3;
struct Seg {
  int l, r;
  lint c;
};
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<Seg> segs(m);
  for (auto& s : segs) {
    std::cin >> s.l >> s.r >> s.c;
    --s.l, --s.r;
  }
  std::sort(segs.begin(), segs.end(), [](Seg a, Seg b) { return a.r < b.r; });
  SegmentTree<lint, lint> seg(
      n, INF, [](lint a, lint b) { return std::min(a, b); },
      [](lint e, lint a) { return std::min(e, a); });
  seg.update(0, 0);
  for (auto s : segs) {
    seg.update(s.r, seg.query(s.l, s.r) + s.c);
  }
  lint ans = seg.query(n - 1, n);
  std::cout << (ans == INF ? -1 : ans) << std::endl;
  return 0;
}
