#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
vector<vector<pair<int, int> > > to(100005);
long long dist[100005];
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M), C(M);
  for (int i = 0; i < (M); ++i) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    R[i]--;
    to[L[i]].emplace_back(R[i], C[i]);
  }
  for (int i = 0; i < (N - 1); ++i) to[i + 1].emplace_back(i, 0);
  for (int i = 0; i < (N); ++i) dist[i] = INF;
  dist[0] = 0;
  priority_queue<pair<long long, int> > q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    for (auto nv : to[v.second]) {
      if (dist[nv.first] > dist[v.second] + nv.second) {
        dist[nv.first] = dist[v.second] + nv.second;
        q.push(make_pair(dist[nv.first], nv.first));
      }
    }
  }
  long long ans = (dist[N - 1] == INF) ? -1 : dist[N - 1];
  cout << ans << endl;
}
