#include <bits/stdc++.h>
using namespace std;
const long long int inf = (1LL << 60);
long long int max(long long int a, long long int b) {
  if (a < b)
    return b;
  else
    return a;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
bool compare(pair<pair<long long int, long long int>, long long int> a,
             pair<pair<long long int, long long int>, long long int> b) {
  if (a.first.first < b.first.first)
    return true;
  else if (a.first.first == b.first.first && a.second < b.second) {
    return true;
  }
  return false;
}
int main() {
  long long int N, K;
  cin >> N >> K;
  auto compareb =
      [](const pair<pair<long long int, long long int>, long long int> a,
         const pair<pair<long long int, long long int>, long long int> b) {
        if (a.second > b.second)
          return true;
        else
          return false;
      };
  vector<pair<pair<long long int, long long int>, long long int>> v(K);
  vector<long long int> ans(N, inf);
  for (int i = 0; i < (K); i++) {
    long long int A, B, C;
    cin >> A >> B >> C;
    A--, B--;
    v[i].first.first = A;
    v[i].first.second = B;
    v[i].second = C;
  }
  sort(v.begin(), v.end(), compare);
  priority_queue<
      pair<pair<long long int, long long int>, long long int>,
      vector<pair<pair<long long int, long long int>, long long int>>,
      decltype(compareb)>
      q(compareb);
  long long int curR = 0, vindex = 0;
  ans[0] = 0;
  while (vindex <= K - 1 && v[vindex].first.first == 0) {
    for (long long int i = curR + 1; i <= v[vindex].first.second; i++) {
      ans[i] = v[vindex].second;
      curR++;
    }
    vindex++;
  }
  curR = 0, vindex = 0;
  cerr << curR << " " << vindex << " " << K << " ";
  while (curR < N - 1) {
    while (vindex <= K - 1 && v[vindex].first.first <= curR) {
      q.push(v[vindex]);
      vindex++;
      cerr << vindex << endl;
    }
    if (q.empty()) break;
    auto b = q.top();
    cerr << b.second << endl;
    q.pop();
    for (long long int i = curR + 1; i <= b.first.second; i++) {
      ans[i] = min(ans[b.first.first] + b.second, ans[i]);
      curR++;
    }
  }
  cerr << ans[N - 2] << ans[N - 3];
  if (ans[N - 1] == inf)
    cout << -1 << endl;
  else
    cout << ans[N - 1] << endl;
  return 0;
}
