#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int L;
  int R;
  long long int C;
  string to_string() {
    char s[100];
    sprintf(s, "{ L: %d, R: %d, C: %lld }", L, R, C);
    return string(s);
  }
} edge_t;
long long int get_shortest_time_on_point(
    int point, map<int, long long int> shortest_time_by_end_point) {
  if (point == 0) {
    return 0LL;
  }
  auto maybe_time_itr = shortest_time_by_end_point.lower_bound(point);
  if (maybe_time_itr == shortest_time_by_end_point.end()) {
    return INT64_MAX;
  } else {
    return maybe_time_itr->second;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  map<int, vector<edge_t>> edges_by_start;
  for (int i = 0; i < M; ++i) {
    int Li, Ri;
    long long int Ci;
    cin >> Li >> Ri >> Ci;
    edges_by_start[Li - 1].push_back(edge_t{Li - 1, Ri - 1, Ci});
  }
  if (edges_by_start.count(0) == 0) {
    cout << -1 << endl;
    return 0;
  }
  map<int, long long int> shortest_time_by_end_point;
  int current_start_point = 0;
  auto maybe_current_edges_itr = edges_by_start.begin();
  while (maybe_current_edges_itr != edges_by_start.end()) {
    vector<edge_t> current_edges = maybe_current_edges_itr->second;
    sort(current_edges.begin(), current_edges.end(),
         [](const edge_t& e1, const edge_t& e2) { return e1.R > e2.R; });
    for (auto edge : current_edges) {
      long long int start_time_on_edge = get_shortest_time_on_point(
          current_start_point, shortest_time_by_end_point);
      long long int shortest_time_by_edge;
      if (start_time_on_edge == INT64_MAX) {
        shortest_time_by_edge = INT64_MAX;
      } else {
        shortest_time_by_edge = start_time_on_edge + edge.C;
      }
      if (shortest_time_by_edge >=
          get_shortest_time_on_point(edge.R, shortest_time_by_end_point)) {
        continue;
      }
      shortest_time_by_end_point[edge.R] = shortest_time_by_edge;
      auto current_end_point_itr = shortest_time_by_end_point.find(edge.R);
      if (current_end_point_itr != shortest_time_by_end_point.begin()) {
        current_end_point_itr--;
      } else {
        continue;
      }
      int current_end_point = current_end_point_itr->first;
      while (current_end_point <= current_start_point) {
        if (shortest_time_by_edge >=
            shortest_time_by_end_point[current_end_point]) {
          break;
        }
        shortest_time_by_end_point.erase(current_end_point_itr);
        current_end_point_itr = shortest_time_by_end_point.find(edge.R);
        if (current_end_point_itr != shortest_time_by_end_point.begin()) {
          current_end_point_itr--;
        } else {
          break;
        }
        current_end_point = current_end_point_itr->first;
      }
    }
    maybe_current_edges_itr = edges_by_start.upper_bound(current_start_point);
    current_start_point = maybe_current_edges_itr->first;
  }
  if (shortest_time_by_end_point.count(N - 1) == 0) {
    cout << -1 << endl;
  } else {
    cout << shortest_time_by_end_point[N - 1] << endl;
  }
  return 0;
}
