#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const double PI = acos(-1);
const long long MOD = 1000000007;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct SegmentTree {
  int N;
  vector<long long> node;
  SegmentTree(vector<long long> v) {
    int sz = v.size();
    N = 1;
    while (N < sz) N *= 2;
    node.assign(2 * N, 1e18);
    for (int i = 0; i < sz; i++) node[i + N] = v[i];
    for (int i = N - 1; i >= 1; i--)
      node[i] = min(node[i << 1 | 0], node[i << 1 | 1]);
  }
  void update(int x, int val) {
    x += N;
    node[x] = val;
    while (x > 1) {
      x >>= 1;
      node[x] = min(node[x << 1 | 0], node[x << 1 | 1]);
    }
  }
  long long getmin(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = N;
    if (r <= a || b <= l) return 1e18;
    if (a <= l && r <= b) return node[k];
    long long vl = getmin(a, b, k << 1 | 0, l, (l + r) / 2);
    long long vr = getmin(a, b, k << 1 | 1, (l + r) / 2, r);
    return min(vl, vr);
  }
  long long getnum(int a) { return node[a + N]; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<tuple<int, int, long long>> Q(M);
  for (int i = 0; i < (int)(M); i++) {
    long long L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    Q[i] = make_tuple(R, L, C);
  }
  sort(Q.begin(), Q.end());
  vector<long long> dist(N, 1e18);
  dist[0] = 0;
  SegmentTree sg(dist);
  for (int i = 0; i < (int)(M); i++) {
    int L, R;
    long long C;
    tie(R, L, C) = Q[i];
    long long d = sg.getmin(L, R);
    if (sg.getnum(R) > d + C) sg.update(R, d + C);
  }
  long long ans = sg.getnum(N - 1);
  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
