#include <bits/stdc++.h>
using namespace std;
long long d[100000];
vector<pair<long long, int> > edges[100000];
void dijikstra(int S, int N) {
  for (int i = 0; i < N; i++) {
    d[i] = LLONG_MAX;
  }
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      pq;
  d[S] = 0;
  pq.push(make_pair(0, S));
  while (!pq.empty()) {
    auto cur = pq.top();
    auto cur_cost = cur.first;
    auto cur_node = cur.second;
    pq.pop();
    if (d[cur_node] < cur_cost) {
      continue;
    }
    for (int i = 0; i < edges[cur_node].size(); i++) {
      int next_node = edges[cur_node][i].second;
      int next_d = d[cur_node] + edges[cur_node][i].first;
      if (d[next_node] > next_d) {
        d[next_node] = next_d;
        pq.push(make_pair(next_d, next_node));
      }
    }
  }
}
int cumsum[100000];
int main(void) {
  int N;
  int M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int L;
    int R;
    long long C;
    cin >> L >> R >> C;
    edges[L - 1].push_back(make_pair(C, R - 1));
    cumsum[L - 1]++;
    cumsum[R - 1]--;
  }
  for (int i = 1; i < N - 1; i++) {
    cumsum[i] += cumsum[i - 1];
  }
  for (int i = 0; i < N - 1; i++) {
    if (cumsum[i] > 0) {
      edges[i + 1].push_back(make_pair(0, i));
    }
  }
  dijikstra(0, N);
  if (d[N - 1] == LLONG_MAX) {
    cout << -1 << endl;
  } else {
    cout << d[N - 1] << endl;
  }
  return 0;
}
