#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
vector<long long> d(100000, 1000000000000000000LL);
struct segTree {
  long long N;
  vector<long long> dat;
  void init(long long n) {
    N = 1;
    while (N < n) N *= 2;
    dat = vector<long long>(2 * N, 1000000000);
  }
  void update(long long k, long long a) {
    k += N - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  long long query(long long a, long long b) {
    if (a > b)
      return query(b, a, 0, 0, N);
    else
      return query(a, b, 0, 0, N);
  }
  long long query(long long a, long long b, long long k, long long l,
                  long long r) {
    if (b <= l || r <= a) return 1000000000000000000LL;
    if (a <= l || r <= b)
      return dat[k];
    else {
      long long m = (l + r) / 2;
      long long vl = query(a, b, k * 2 + 1, l, m);
      long long vr = query(a, b, k * 2 + 1, m, r);
      return min(vl, vr);
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long>> lrc(m);
  for (int i = 0; i < ((int)m); i++) {
    cin >> lrc[i].first.first >> lrc[i].first.second >> lrc[i].second;
    lrc[i].first.first--;
    lrc[i].first.second--;
  }
  sort(lrc.begin(), lrc.end());
  segTree seg;
  seg.init(n);
  for (int i = 0; i < ((int)m); i++) {
    long long l = lrc[i].first.first;
    long long r = lrc[i].first.second;
    long long c = lrc[i].second;
    long long k = seg.query(l, r + 1);
    seg.update(r, k);
  }
  long long ans = seg.query(n - 1, n);
  if (ans == 1000000000000000000LL)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
