#include <bits/stdc++.h>
using namespace std;
long long dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
long long kaijo[200010];
struct edge {
  long long to, cost;
};
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool prime(long long a) {
  if (a == 1) return false;
  for (long long i = 2; i * i <= a; i++) {
    if (a % i == 0) return false;
  }
  return true;
}
void init_fact(long long n) {
  kaijo[0] = 1;
  for (long long i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= 998244353;
  }
}
long long modpow(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2) return modpow(a, b - 1) * a % 998244353;
  long long memo = modpow(a, b / 2);
  return memo * memo % 998244353;
}
long long comb(long long a, long long b) {
  return kaijo[a] * modpow(kaijo[a - b], 998244353 - 2) % 998244353 *
         modpow(kaijo[b], 998244353 - 2) % 998244353;
}
long long inv(long long x) {
  x = modpow(x, 998244353 - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
double dist(double ax, double ay, double bx, double by) {
  return sqrt((bx - ax) * (bx - ax) + (ay - by) * (ay - by));
}
long long n, m, d[100010], le;
pair<pair<long long, long long>, long long> p[100010];
set<long long> st;
signed main() {
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    cin >> p[i].first.first >> p[i].first.second >> p[i].second;
    p[i].first.first--, p[i].first.second--;
  }
  sort(p, p + m);
  fill(d, d + n, 1000000000);
  d[0] = 0;
  st.insert(0);
  for (long long i = 0; i < m; i++) {
    long long le = *st.lower_bound(p[i].first.first);
    d[p[i].first.second] = min(d[p[i].first.second], d[le] + p[i].second);
    st.insert(p[i].first.second);
  }
  cout << (d[n - 1] == 1000000000 ? -1 : d[n - 1]) << endl;
  return 0;
}
