#include <bits/stdc++.h>
using ll = long long;
using P = std::pair<ll, ll>;
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}
ll pow_mod(ll a, ll b, ll mod = -1) {
  if ((a == 0) || (mod != -1 && a % mod == 0)) {
    return 0;
  }
  ll x = 1;
  while (b > 0) {
    if (b & 1) {
      x = (mod != -1) ? (x * a) % mod : x * a;
    }
    a = (mod != -1) ? (a * a) % mod : a * a;
    b >>= 1;
  }
  return x;
}
const ll MOD = 1e9 + 7;
class Edge {
 public:
  ll l, r, cost;
};
template <class T>
class SegmentTree {
 private:
  long long n;
  T init;
  std::vector<T> dat;
  std::function<T(T, T)> fn;

 public:
  SegmentTree(long long i, T para, std::function<T(T, T)> fun)
      : init(para), fn(fun) {
    n = 1;
    while (n < i) {
      n *= 2;
    }
    dat = std::vector<T>(2 * n - 1, init);
  }
  void update(long long k, T a, bool dest) {
    k += n - 1;
    if (dest)
      dat[k] = a;
    else
      dat[k] = fn(dat[k], a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = fn(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  T query(long long a, long long b) {
    assert(0 <= a && b <= n);
    T val = init;
    long long l = a + n - 1, r = b + n - 1;
    for (; l < r; l = (l >> 1), r = (r >> 1)) {
      if (!(r & 1)) {
        r--;
        val = fn(val, dat[r]);
      }
      if (!(l & 1)) {
        val = fn(val, dat[l]);
        l++;
      }
    }
    return val;
  }
};
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll n, m;
  std::cin >> n >> m;
  std::vector<Edge> edge(m);
  for (ll(i) = (0); i < (m); i++) {
    ll l, r, c;
    std::cin >> l >> r >> c;
    l--;
    r--;
    edge[i] = {l, r, c};
  }
  std::sort(edge.begin(), edge.end(),
            [](Edge t1, Edge t2) { return t1.r < t2.r; });
  ll itr = 0;
  SegmentTree<ll> seg(n, LLONG_MAX, [](ll a, ll b) { return std::min(a, b); });
  seg.update(0, 0, true);
  for (ll(i) = (1); i < (n); i++) {
    while (itr != n && edge[itr].r == i) {
      if (seg.query(edge[itr].l, edge[itr].r) != LLONG_MAX) {
        seg.update(i, seg.query(edge[itr].l, edge[itr].r) + edge[itr].cost,
                   false);
      }
      itr++;
    }
  }
  if (seg.query(n - 1, n) == LLONG_MAX)
    std::cout << -1;
  else
    std::cout << seg.query(n - 1, n);
  return 0;
}
