#include <bits/stdc++.h>
using namespace std;
long long a7 = 1000000007;
long long inf = 1000000000000000;
long long min(long long a, long long b) {
  if (a < b) return a;
  return b;
}
long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
int main() {
  long long n, m;
  cin >> n >> m;
  long long l[m], r[m], c[m];
  vector<pair<long long, long long>> p;
  for (long long i = 0; i <= m - 1; i++) {
    cin >> l[i] >> r[i] >> c[i];
    p.push_back(make_pair(l[i], i));
  }
  sort(p.begin(), p.end());
  vector<vector<long long>> v(n + 1);
  priority_queue<long long, vector<long long>, greater<long long>> q;
  q.push(1);
  long long a;
  long long mini[n + 1];
  for (long long i = 1; i <= n; i++) {
    mini[i] = inf;
  }
  mini[1] = 0;
  long long cc = 0;
  long long la, ra;
  while (q.size() > 0) {
    a = q.top();
    q.pop();
    while (cc <= m - 1 && a >= p[cc].first) {
      if (mini[a] + c[p[cc].second] < mini[r[p[cc].second]]) {
        mini[r[p[cc].second]] = mini[a] + c[p[cc].second];
        q.push(r[p[cc].second]);
      }
      cc++;
    }
  }
  if (mini[n] == inf) {
    cout << -1 << endl;
  } else {
    cout << mini[n] << endl;
  }
  return 0;
}
