#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const double EPS = 1e-7;
const int INF = 1 << 30;
const long long int LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct LazyRMQ {
 private:
  long long int n;
  vector<long long int> node, lazy;
  vector<bool> lazyFlag;

 public:
  LazyRMQ(vector<long long int> v) {
    long long int sz = (long long int)v.size();
    n = 1;
    while (n < sz) {
      n *= 2;
    }
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, LLINF);
    lazyFlag.resize(2 * n - 1, false);
    for (long long int i = 0; i < sz; i++) {
      node[i + n - 1] = v[i];
    }
    for (long long int i = n - 2; i >= 0; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  void eval(long long int now, long long int l, long long int r) {
    if (lazyFlag[now]) {
      node[now] = lazy[now];
      if (r - l > 1) {
        lazy[2 * now + 1] = lazy[2 * now + 2] = lazy[now];
        lazyFlag[2 * now + 1] = lazyFlag[2 * now + 2] = true;
      }
      lazyFlag[now] = false;
    }
  }
  void update(long long int a, long long int b, long long int x,
              long long int now = 0, long long int l = 0,
              long long int r = -1) {
    if (r < 0) {
      r = n;
    }
    eval(now, l, r);
    if (b <= l || r <= a) {
      return;
    }
    if (a <= l && r <= b) {
      lazy[now] = x;
      lazyFlag[now] = true;
      eval(now, l, r);
    } else {
      update(a, b, x, 2 * now + 1, l, (l + r) / 2);
      update(a, b, x, 2 * now + 2, (l + r) / 2, r);
      node[now] = min(node[2 * now + 1], node[2 * now + 2]);
    }
  }
  long long int getmin(long long int a, long long int b, long long int now = 0,
                       long long int l = 0, long long int r = -1) {
    if (r < 0) {
      r = n;
    }
    eval(now, l, r);
    if (b <= l || r <= a) {
      return LLINF;
    }
    if (a <= l && r <= b) {
      return node[now];
    }
    long long int vl = getmin(a, b, 2 * now + 1, l, (l + r) / 2);
    long long int vr = getmin(a, b, 2 * now + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int N, M;
  cin >> N >> M;
  vector<pair<pair<long long int, long long int>, long long int>> LR(M);
  for (long long int i = 0; i < M; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    LR[i] = make_pair(make_pair(a, b), c);
  }
  sort((LR).begin(), (LR).end());
  LazyRMQ dp(vector<long long int>(101010, LLINF));
  dp.update(0, 1, 0);
  for (long long int i = 0; i < M; i++) {
    long long int l = LR[i].first.first;
    long long int r = LR[i].first.second;
    long long int c = LR[i].second;
    dp.update(l + 1, r + 1, dp.getmin(l, l + 1) + c);
  }
  long long int ans = dp.getmin(N - 1, N);
  cout << (ans == LLINF ? -1 : ans) << "\n";
}
