#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long> divisor(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
struct LazySegmentTree {
 private:
  int n;
  vector<int> node, lazy;
  vector<bool> lazyFlag;

 public:
  LazySegmentTree(vector<int> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, INT_MAX);
    lazyFlag.resize(2 * n - 1, false);
    for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void lazyEvaluate(int k, int l, int r) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return INT_MAX;
    if (a <= l && r <= b) return node[k];
    int vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
class UnionFind {
 public:
  vector<long long> par;
  vector<long long> siz;
  UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) {
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(long long sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  long long root(long long x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool issame(long long x, long long y) { return root(x) == root(y); }
  long long size(long long x) { return siz[root(x)]; }
};
struct segtree {
  long long N, dat[4 * 200004];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n) N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) dat[i] = LLONG_MAX;
  }
  void update(int 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(int a, int b) { return query(a, b, 0, 0, N); }
  long long query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return LLONG_MAX;
    if (a <= l && r <= b) return dat[k];
    int m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
};
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}
class BIT {
 public:
  vector<int> bit;
  int M;
  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}
  int sum(int i) {
    if (i <= 0) return 0;
    if (i > M) return sum(M);
    if (!i) return 0;
    return bit[i] + sum(i - (i & -i));
  }
  void add(int i, int x) {
    if (i > M) return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};
map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1) ret[n] = 1;
  return ret;
}
long long mod = 998244353;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  segtree se(n + 1);
  pair<pair<int, int>, long long> edge[100004];
  for (int i = 0; i < m; i++) {
    cin >> edge[i].first.first >> edge[i].first.second >> edge[i].second;
  }
  sort(edge, edge + m);
  se.update(1, 0);
  for (int i = 0; i < m; i++) {
    pair<int, int> now = edge[i].first;
    long long tmp = se.query(now.first, now.second);
    if (tmp == LLONG_MAX) {
      cout << -1;
      return 0;
    }
    long long genjou = se.query(now.second, now.second + 1);
    if (tmp + edge[i].second < genjou) {
      se.update(now.second, tmp + edge[i].second);
    }
  }
  long long ans = se.query(n, n + 1);
  if (ans == LLONG_MAX) {
    cout << -1;
  } else {
    cout << ans;
  }
}
