#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
using VL = V<long long>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<long long, long long>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T &a, T b);
template <typename T>
inline bool chmin(T &a, T b);
void print_ints(vector<long long> v);
template <typename T>
void drop(T a);
const long long INF = 1e18;
void solve() {
  long long N, M;
  cin >> N >> M;
  V<V<PL>> edges(N);
  for (long long i = 0; i < (N - 1); ++i) edges[i + 1].emplace_back(i, 0);
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    edges[l].emplace_back(r, c);
  }
  VL dist(N, INF);
  dist[0] = 0;
  priority_queue<PL, VPL, greater<PL>> q;
  q.push({0, 0});
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    long long cost = p.first;
    long long u = p.second;
    if (cost > dist[u]) continue;
    for (auto vp : edges[u]) {
      long long v = vp.first;
      long long edge_cost = vp.second;
      long long n_cost = cost + edge_cost;
      if (chmin(dist[v], n_cost)) {
        q.push({n_cost, v});
      }
    }
  }
  long long res = dist[N - 1] == INF ? -1 : dist[N - 1];
  cout << res << endl;
}
template <class Monoid, class Action>
struct LazySegTree {
  using FuncMonoid = function<Monoid(Monoid, Monoid)>;
  using FuncAction = function<void(Monoid &, Action)>;
  using FuncLazy = function<void(Action &, Action)>;
  FuncMonoid FM;
  FuncAction FA;
  FuncLazy FL;
  Monoid UNITY_MONOID;
  Action UNITY_LAZY;
  long long SIZE, HEIGHT;
  vector<Monoid> dat;
  vector<Action> lazy;
  LazySegTree(long long n, const FuncMonoid fm, const FuncAction fa,
              const FuncLazy fl, const Monoid &unity_monoid,
              const Action &unity_lazy)
      : FM(fm),
        FA(fa),
        FL(fl),
        UNITY_MONOID(unity_monoid),
        UNITY_LAZY(unity_lazy) {
    SIZE = 1;
    HEIGHT = 0;
    while (SIZE < n) SIZE <<= 1, ++HEIGHT;
    dat.assign(SIZE * 2, UNITY_MONOID);
    lazy.assign(SIZE * 2, UNITY_LAZY);
  }
  void init(long long n, const FuncMonoid fm, const FuncAction fa,
            const FuncLazy fl, const Monoid &unity_monoid,
            const Action &unity_lazy) {
    FM = fm;
    FA = fa;
    FL = fl;
    UNITY_MONOID = unity_monoid;
    UNITY_LAZY = unity_lazy;
    SIZE = 1;
    HEIGHT = 0;
    while (SIZE < n) SIZE <<= 1, ++HEIGHT;
    dat.assign(SIZE * 2, UNITY_MONOID);
    lazy.assign(SIZE * 2, UNITY_LAZY);
  }
  void set(long long a, const Monoid &v) { dat[a + SIZE] = v; }
  void build() {
    for (long long k = SIZE - 1; k > 0; --k)
      dat[k] = FM(dat[k * 2], dat[k * 2 + 1]);
  }
  inline void evaluate(long long k) {
    if (lazy[k] == UNITY_LAZY) return;
    if (k < SIZE) FL(lazy[k * 2], lazy[k]), FL(lazy[k * 2 + 1], lazy[k]);
    FA(dat[k], lazy[k]);
    lazy[k] = UNITY_LAZY;
  }
  inline void update(long long a, long long b, const Action &v, long long k,
                     long long l, long long r) {
    evaluate(k);
    if (a <= l && r <= b)
      FL(lazy[k], v), evaluate(k);
    else if (a < r && l < b) {
      update(a, b, v, k * 2, l, (l + r) >> 1),
          update(a, b, v, k * 2 + 1, (l + r) >> 1, r);
      dat[k] = FM(dat[k * 2], dat[k * 2 + 1]);
    }
  }
  inline void update(long long a, long long b, const Action &v) {
    update(a, b, v, 1, 0, SIZE);
  }
  inline Monoid get(long long a, long long b, long long k, long long l,
                    long long r) {
    evaluate(k);
    if (a <= l && r <= b)
      return dat[k];
    else if (a < r && l < b)
      return FM(get(a, b, k * 2, l, (l + r) >> 1),
                get(a, b, k * 2 + 1, (l + r) >> 1, r));
    else
      return UNITY_MONOID;
  }
  inline Monoid get(long long a, long long b) { return get(a, b, 1, 0, SIZE); }
  inline Monoid operator[](long long a) { return get(a, a + 1); }
};
void solve2() {
  long long N, M;
  cin >> N >> M;
  VL dist(N, INF);
  auto fm = [](long long a, long long b) { return min(a, b); };
  auto fa = [](long long &a, long long b) { a = min(a, b); };
  auto fl = [](long long &a, long long b) { a = min(a, b); };
  LazySegTree<long long, long long> st(N + 1, fm, fa, fl, INF, INF);
  st.update(0, 1, 0);
  V<P<PL, long long>> edges(M);
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    edges[i] = {PL(l, r), c};
  }
  sort(edges.begin(), edges.end());
  for (auto &e : edges) {
    long long l = e.first.first;
    long long r = e.first.second;
    long long c = e.second;
    long long min_of_range = st.get(l, r + 1);
    st.update(l, r + 1, min_of_range + c);
  }
  long long res = st.get(N - 1, N);
  if (res == INF) drop(-1);
  cout << res << endl;
}
void solve3() {
  long long N, M;
  cin >> N >> M;
  V<VPL> ls(N);
  V<VPL> rs(N);
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    ls[l].push_back(PL(r, c));
    rs[r].push_back(PL(l, c));
  }
  VL dist(N, INF);
  dist[0] = 0;
  multiset<long long> costs;
  for (long long i = 0; i < (N); ++i) {
    if (!costs.empty()) {
      dist[i] = *costs.cbegin();
    }
    if (dist[i] != INF) {
      for (auto e : ls[i]) {
        costs.insert(dist[i] + e.second);
      }
    }
    for (auto e : rs[i]) {
      costs.erase(dist[e.first] + e.second);
    }
  }
  long long res = dist[N - 1] == INF ? -1 : dist[N - 1];
  cout << res << endl;
}
struct exit_exception : public std::exception {
  const char *what() const throw() { return "Exited"; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  try {
    solve3();
  } catch (exit_exception &e) {
  }
  return 0;
}
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print_ints(vector<long long> v) {
  for (long long i = 0; i < (v.size()); ++i) {
    if (i > 0) {
      cout << " ";
    }
    cout << v[i];
  }
  cout << endl;
}
template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}
