#include <bits/stdc++.h>
using namespace std;
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> start_pos(N), end_pos(N);
  vector<long long int> costs(M);
  for (int i = 0; i < (int)(M); i++) {
    int l, r;
    long long int c;
    cin >> l >> r >> c;
    l--, r--;
    start_pos[l].emplace_back(i);
    end_pos[r].emplace_back(i);
    costs[i] = c;
  }
  vector<long long int> dist(N);
  dist[0] = 0;
  set<pair<int, int>> intervals;
  vector<pair<int, int>> memo(M);
  for (int idx : start_pos[0]) {
    intervals.emplace(costs[idx], idx);
    memo[idx] = make_pair(costs[idx], idx);
  }
  for (int i = 1; i < (int)(N); i++) {
    if (intervals.empty()) {
      cout << -1 << endl;
      return 0;
    }
    dist[i] = intervals.begin()->first;
    for (int idx : end_pos[i]) {
      intervals.erase(memo[idx]);
    }
    for (int idx : start_pos[i]) {
      intervals.emplace(dist[i] + costs[idx], idx);
      memo[idx] = make_pair(dist[i] + costs[idx], idx);
    }
  }
  cout << dist[N - 1] << endl;
  return 0;
}
