#include <bits/stdc++.h>
using namespace std;
int n, M;
pair<int, pair<int, int>> P[100000];
vector<long long int> value;
int N;
void update(int i, long long int x) {
  i += N - 1;
  value[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    value[i] = min(value[i * 2 + 1], value[i * 2 + 2]);
  }
}
long long int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return 10000000000000;
  if (a <= l && r <= b)
    return value[k];
  else {
    long long int c1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    long long int c2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(c1, c2);
  }
}
int main() {
  cin >> n >> M;
  N = 1;
  while (N < n) N *= 2;
  value = vector<long long int>(2 * N - 1, 10000000000000);
  update(0, 0);
  for (int i = 0; i < (int)(M); i++) {
    cin >> P[i].first >> P[i].second.first >> P[i].second.second;
  }
  sort(P, P + M);
  for (int i = 0; i < (int)(M); i++) {
    int L = P[i].first - 1;
    int R = P[i].second.first - 1;
    long long int C = P[i].second.second;
    update(R, min(value[R + N - 1], query(L, R, 0, 0, N) + C));
  }
  if (value[n + N - 2] == 10000000000000) value[n + N - 2] = -1;
  cout << value[n + N - 2] << endl;
}
