#include <bits/stdc++.h>
using namespace std;
long long seg[1000005];
const long long INF = 1e17;
int nn;
long long dp[100005];
vector<pair<pair<int, int>, long long> > l;
void init(int _n) {
  nn = 1;
  while (nn < _n) nn *= 2;
  for (int i = 0; i < 2 * nn - 1; i++) seg[i] = INF;
}
void update(int k, int a) {
  k += nn - 1;
  seg[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    seg[k] = min(seg[2 * k + 1], seg[2 * k + 2]);
  }
}
long long query(int a, int b, int k, int l, int r) {
  if (b <= l || r <= a) return INF;
  if (a <= l && r <= b)
    return seg[k];
  else {
    long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    l.push_back(pair<pair<int, int>, long long>(pair<int, int>(L, R), C));
  }
  sort(l.begin(), l.end());
  update(0, 0);
  for (int i = 0; i < l.size(); i++) {
    pair<int, int> LR = l[i].first;
    long long tmp = query(LR.first, LR.second, 0, 0, nn);
    if (tmp == INF) continue;
    update(LR.second, min(seg[LR.second + nn - 1], tmp + l[i].second));
  }
  if (seg[nn + n - 2] == INF)
    cout << -1 << endl;
  else
    cout << seg[nn + n - 2] << endl;
}
