#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long>;
using pii = pair<long long, long long>;
using vpii = vector<pii>;
using ll = long long;
constexpr long long TEN(long long n) { return n == 0 ? 1 : 10 * TEN(n - 1); }
template <typename T>
bool chmin(T& a, T b) {
  return a > b ? (a = b, 1) : 0;
}
template <typename T>
bool chmax(T& a, T b) {
  return a < b ? (a = b, 1) : 0;
}
long long read() {
  long long a;
  scanf("%lld", &a);
  return a;
}
const long double pi = acos(-1);
constexpr long long inf = 3 * TEN(18) + 10;
const long long mod = TEN(9) + 7;
long long N, M;
pii rng[100010];
vector<pii> in[100010], out[100010];
long long d[100010];
signed main() {
  cin >> N >> M;
  for (long long i = 0; i < (long long)(M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    rng[i] = pii(l, r);
    in[l].emplace_back(c, i);
    out[r].emplace_back(c, i);
  }
  for (long long i = 0; i < (long long)(N); ++i) d[i] = inf;
  d[0] = 0;
  set<pii> V;
  for (long long i = 0; i < (long long)(N); ++i) {
    if (V.size()) d[i] = V.begin()->first;
    for (pii p : out[i]) {
      long long id = p.second;
      long long v = d[rng[id].first] + p.first;
      V.erase(pii(v, id));
    }
    for (pii p : in[i]) {
      long long c = p.first, id = p.second;
      long long v = d[rng[id].first] + c;
      V.insert(pii(v, id));
    }
  }
  if (d[N - 1] == inf) {
    puts("-1");
  } else {
    printf("%lld\n", d[N - 1]);
  }
}
