#include <bits/stdc++.h>
using namespace std;
long long N, M;
vector<pair<pair<long long, long long>, long long>> LRC;
long long dp[101010];
deque<pair<long long, long long>> sld;
long long i, j, k, l;
int main() {
  scanf("%lld%lld", &N, &M);
  for (i = 0; i < M; i++) {
    long long l, r, c;
    scanf("%lld%lld%lld", &l, &r, &c);
    LRC.push_back({{l, r}, c});
  }
  sort(LRC.begin(), LRC.end());
  for (i = 0; i < N; i++) {
    dp[i + 1] = 1e15;
  }
  dp[1] = 0;
  j = 0;
  long long ans = 0;
  for (i = 0; i < N; i++) {
    while (j < M && LRC[j].first.first == i + 1) {
      while (sld.size() && sld.back().first > LRC[j].first.second &&
             sld.back().second > LRC[j].second) {
        sld.pop_back();
      }
      sld.push_back({LRC[j].first.second, LRC[j].second});
      j++;
    }
    if (dp[i + 1] == 1e15) continue;
    while (sld.size()) {
      pair<long long, long long> a = sld.front();
      dp[a.first] = min(dp[a.first], dp[i + 1] + a.second);
      sld.pop_front();
    }
  }
  if (dp[N] == 1e15) {
    printf("-1");
  } else
    printf("%lld", dp[N]);
  return 0;
}
