#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> edge[N];
  for (int i = 0; i < M; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    edge[l - 1].emplace_back(make_pair(r - 1, c));
  }
  long long dist[N];
  for (int i = 0; i < N; i++) dist[i] = LLONG_MAX / 2;
  priority_queue<pair<long long, int>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pair<int, int> p = pq.top();
    pq.pop();
    long long d = -p.first;
    int cur = p.second;
    if (d > dist[cur]) continue;
    if (cur == N - 1) {
      cout << d << endl;
      return 0;
    }
    for (int i = 0; i < (int)edge[cur].size(); i++) {
      pair<int, int> e = edge[cur][i];
      long long cost = e.second;
      int next = e.first;
      if (dist[next] > d + cost) {
        dist[next] = d + cost;
        pq.push(make_pair(-d - cost, next));
      }
    }
    if (cur != 0 && dist[cur - 1] > d) {
      dist[cur - 1] = d;
      pq.push(make_pair(-d, cur - 1));
    }
  }
  cout << -1 << endl;
  return 0;
}
