#include <bits/stdc++.h>
using namespace std;
const long long MAX = 262144;
long long mp[MAX * 2 - 1];
void setup() {
  for (long long i = 0; i < MAX * 2; i++) mp[i] = 1000000010;
}
void update(long long i, long long x) {
  i += MAX - 1;
  mp[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    mp[i] = min(mp[i * 2 + 1], mp[i * 2 + 2]);
  }
}
long long query(long long a, long long b, long long k, long long l,
                long long r) {
  if (r <= a || b <= l) return 1000000010;
  if (a <= l && r <= b)
    return mp[k];
  else {
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
signed main() {
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  pair<pair<long long, long long>, long long> hen[100000];
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    hen[i].first.first = --a;
    hen[i].first.second = --b;
    hen[i].second = c;
  }
  sort(hen, hen + m);
  setup();
  update(0, 0);
  for (long long i = 0; i < m; i++) {
    long long num = query(hen[i].first.first, hen[i].first.second, 0, 0, MAX);
    update(hen[i].first.second,
           min(num + hen[i].second, mp[hen[i].first.second + MAX - 1]));
  }
  long long answ = query(n - 1, MAX, 0, 0, MAX);
  if (answ == 1000000010)
    cout << -1 << "\n";
  else
    cout << answ << "\n";
  return 0;
}
