#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long start = 1;
long long n;
vector<vector<pair<long long, long long>>> G;
vector<long long> dist;
void dijkstra() {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  dist = vector<long long>(n + 1, INF);
  dist[start] = 0;
  pq.push(pair<long long, long long>(0, start));
  while (!pq.empty()) {
    int d = pq.top().first, v = pq.top().second;
    pq.pop();
    if (v == n) break;
    if (dist[v] < d) continue;
    for (int i = 0; i < (int)G[v].size(); ++i) {
      pair<long long, long long> e = G[v][i];
      if (d + e.second < dist[e.first]) {
        dist[e.first] = d + e.second;
        pq.push(pair<long long, long long>(dist[e.first], e.first));
      }
    }
  }
}
int main() {
  long long m, i, j, k;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long>> v(m);
  for (i = 0; i < m; i++) {
    cin >> v[i].first.second >> v[i].second >> v[i].first.first;
  }
  sort(v.begin(), v.end());
  G = vector<vector<pair<long long, long long>>>(n + 1);
  dist.resize(n + 1);
  set<pair<long long, long long>> st;
  for (i = 0; i < m; i++) {
    if (st.find({v[i].first.second, v[i].second}) != st.end()) continue;
    G[v[i].first.second].push_back({v[i].second, v[i].first.first});
    st.insert({v[i].first.second, v[i].second});
  }
  for (i = 2; i <= n; i++) {
    G[i].push_back({i - 1, 0});
  }
  dijkstra();
  if (dist[n] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[n] << endl;
  return 0;
}
