#include <bits/stdc++.h>
using namespace std;
vector<pair<string, pair<int, int>>> vec;
const int MAX_N = 1 << 20;
int n, dat[2 * MAX_N - 1];
void init(int n_) {
  n = 1;
  while (n < n_) n *= 2;
  for (int i = 0; i < 2 * n - 1; i++) dat[i] = INT_MAX;
}
void update(int k, int 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]);
  }
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
vector<int> dp(100001, INT_MAX);
int main() {
  int M;
  int N;
  scanf("%d", &N);
  init(N);
  scanf("%d", &M);
  vector<int> C(M - 1 + 1);
  vector<int> L(M - 1 + 1);
  vector<int> R(M - 1 + 1);
  vector<vector<int>> lv(M, vector<int>(3));
  for (int i = 0; i <= M - 1; i++) {
    scanf("%d", &L[i]);
    scanf("%d", &R[i]);
    scanf("%d", &C[i]);
    L[i]--;
    R[i]--;
    lv[i][0] = L[i];
    lv[i][1] = R[i];
    lv[i][2] = C[i];
  }
  dp[0] = 0;
  update(0, 0);
  sort(lv.begin(), lv.end());
  for (long long i = 0; i < M; i++) {
    int l = lv[i][0], r = lv[i][1], c = lv[i][2];
    dp[r] = min(dp[r], query(l, r + 1, 0, 0, n) + c);
    update(r, dp[r]);
  }
  if (dp[N - 1] >= INT_MAX)
    printf("-1\n");
  else
    printf("%d\n", dp[N - 1]);
  return 0;
}
