#include <bits/stdc++.h>
using namespace std;
int sqrtN = 318;
struct SqrtDecomposition {
  int N, K;
  vector<long long> data;
  vector<long long> bucketUpdatedLazy;
  vector<long long> bucketMin;
  SqrtDecomposition(int n) {
    N = n;
    K = (N + sqrtN - 1) / sqrtN;
    data.assign(N + sqrtN, LLONG_MAX);
    bucketUpdatedLazy.assign(K, -1);
    bucketMin.assign(K, ((long long)1 << 31) - 1);
  }
  void propagateLazy(int k) {
    if (bucketUpdatedLazy[k] < 0) {
      return;
    }
    for (int i = 0; i < sqrtN; i++) {
      data[k * sqrtN + i] = min(data[k * sqrtN + i], bucketUpdatedLazy[k]);
    }
    bucketMin[k] = min(bucketMin[k], bucketUpdatedLazy[k]);
    bucketUpdatedLazy[k] = -1;
  }
  void updateMin(int k) {
    bucketMin[k] = data[k * sqrtN];
    for (int i = 0; i < sqrtN; i++) {
      bucketMin[k] = min(data[k * sqrtN + i], bucketMin[k]);
    }
  }
  long long get(int x) {
    propagateLazy(x / sqrtN);
    return data[x];
  }
  long long find(int x, int y) {
    propagateLazy(x / sqrtN);
    propagateLazy((y - 1) / sqrtN);
    long long ans = data[x];
    if (y - x < sqrtN) {
      for (int i = x; i < y; i++) {
        ans = min(ans, data[i]);
      }
      return ans;
    }
    for (int i = x / sqrtN + 1; i < y / sqrtN; i++) {
      ans = min(ans, bucketMin[i]);
    }
    for (int i = x; i < (x / sqrtN + 1) * sqrtN; i++) {
      ans = min(ans, data[i]);
    }
    for (int i = y / sqrtN * sqrtN; i < y; i++) {
      ans = min(ans, data[i]);
    }
    return ans;
  }
  void update(int x, int y, long long a) {
    if (y - x < sqrtN) {
      propagateLazy(x / sqrtN);
      propagateLazy((y - 1) / sqrtN);
      for (int i = x; i < y; i++) {
        data[i] = min(data[i], a);
      }
      updateMin(x / sqrtN);
      updateMin((y - 1) / sqrtN);
      return;
    }
    for (int i = x / sqrtN + 1; i < y / sqrtN; i++) {
      bucketUpdatedLazy[i] = a;
      bucketMin[i] = min(bucketMin[i], a);
    }
    propagateLazy(x / sqrtN);
    for (int i = x; i < (x / sqrtN + 1) * sqrtN; i++) {
      data[i] = min(data[i], a);
    }
    updateMin(x / sqrtN);
    propagateLazy((y - 1) / sqrtN);
    for (int i = y / sqrtN * sqrtN; i < y; i++) {
      data[i] = min(data[i], a);
    }
    updateMin((y - 1) / sqrtN);
  }
};
pair<int, pair<int, long long> > LRC[100000];
int main() {
  int N;
  int M;
  cin >> N >> M;
  SqrtDecomposition sq(N);
  for (int i = 0; i < M; i++) {
    int L;
    int R;
    int C;
    cin >> L >> R >> C;
    LRC[i] = make_pair(L, make_pair(R, C));
  }
  sort(LRC, LRC + N);
  sq.data[0] = 0;
  for (int i = 0; i < N; i++) {
    int L = LRC[i].first;
    int R = LRC[i].second.first;
    int C = LRC[i].second.second;
    long long range_min = sq.find(L - 1, R);
    if (range_min == LLONG_MAX) {
      continue;
    }
    sq.update(L - 1, R, C + range_min);
  }
  if (sq.data[N - 1] != LLONG_MAX) {
    cout << sq.data[N - 1] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
