#include <bits/stdc++.h>
using namespace std;
vector<long long> dist;
vector<vector<pair<long long, long long>>> AdjList;
const long long inf = 1e9 + 7;
void dijkstra(long long s) {
  dist[s] = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  pq.push(pair<long long, long long>(0, s));
  while (!pq.empty()) {
    pair<long long, long long> front = pq.top();
    pq.pop();
    long long d = front.first;
    long long u = front.second;
    if (d == dist[u]) {
      for (long long j = 0; j < (long long)AdjList[u].size(); j++) {
        pair<long long, long long> v = AdjList[u][j];
        if (dist[u] + v.second < dist[v.first]) {
          dist[v.first] = dist[u] + v.second;
          pq.push(pair<long long, long long>(dist[v.first], v.first));
        }
      }
    }
  }
}
signed main() {
  long long n, m;
  cin >> n >> m;
  AdjList.assign(n, vector<pair<long long, long long>>());
  dist.assign(n, inf);
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    AdjList[u].push_back(pair<long long, long long>(v, w));
    for (long long j = u + 1; j < v; j++) {
      AdjList[v].push_back(pair<long long, long long>(j, 0));
    }
  }
  dijkstra(0);
  if (dist[n - 1] == inf)
    cout << -1;
  else
    cout << dist[n - 1];
}
