#include <bits/stdc++.h>
using namespace std;
struct Benri {
  Benri() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
  }
} benri;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using Real = double;
using Point = complex<Real>;
template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using minPQ = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MOD = 1000000007;
constexpr double INF = 1e10;
constexpr Real EPS = 1e-10;
const Real PI = acos(-1);
using number = double;
int main() {
  int n, m;
  cin >> n >> m;
  vi l(n), r(n);
  vll c(n);
  vvi hsl(n), hsr(n);
  for (int k = 0; k < (int)(m); k++) {
    cin >> l[k] >> r[k] >> c[k];
    l[k]--;
    r[k]--;
    hsl[l[k]].push_back(k);
    hsr[r[k]].push_back(k);
  }
  vll dist(n);
  multiset<ll> st;
  for (int i = 0; i < (int)(n); i++) {
    if (i != 0) {
      if (st.empty()) {
        cout << -1 << endl;
        return 0;
      }
      dist[i] = *st.begin();
    }
    for (auto k : hsl[i]) {
      st.insert(dist[l[k]] + c[k]);
    }
    for (auto k : hsr[i]) {
      st.erase(st.lower_bound(dist[l[k]] + c[k]));
    }
  }
  cout << dist[n - 1] << endl;
}
