#include <bits/stdc++.h>
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 50;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
template <typename T>
struct edge {
  int from, to;
  T cost;
  edge(int to, T cost) : from(0), to(to), cost(cost) {}
  edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};
template <typename T>
struct SegmentTree {
 private:
  int n;
  vector<T> node;
  T MAXV = numeric_limits<T>::max();

 public:
  SegmentTree(vector<T> v) {
    int sz = v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1, MAXV);
    for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
  }
  void update(int k, T val) {
    k += (n - 1);
    node[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  T getv(int k) {
    k += n - 1;
    return node[k];
  }
  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || l >= b) return MAXV;
    if (a <= l && b >= r) return node[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
ll dp[100005];
void solve() {
  int n, m;
  cin >> n >> m;
  vector<ll> v(n, INF);
  SegmentTree<ll> sg(v);
  vector<pair<pair<int, int>, ll>> d;
  for (int i = (int)0; i < (int)m; ++i) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    l--;
    r--;
    d.emplace_back(make_pair(l, r), c);
  }
  sort(d.begin(), d.end(),
       [](pair<pair<int, int>, ll>& l, pair<pair<int, int>, ll>& r) {
         return l.first.second < r.first.second;
       });
  sg.update(d.front().first.second, d.front().second);
  for (int i = (int)0; i < (int)m; ++i) {
    ll a = sg.query(d[i].first.first, d[i].first.second);
    ll b = sg.getv(d[i].first.second);
    sg.update(d[i].first.second, min(a + d[i].second, b));
  }
  ll ans = sg.getv(n - 1);
  if (ans >= INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
