#include <bits/stdc++.h>
using namespace std;
long long D[100010];
pair<pair<long long, long long>, long long> A[100010];
long long L[100010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  for (long long i = 0; i < 100010; i++) L[i] = 1000000000000000000;
  L[0] = 0;
  for (long long i = 0; i < M; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    A[i] = {{r, l}, c};
  }
  sort(A, A + M);
  for (long long i = 0; i < M; i++) {
    long long s = A[i].first.second;
    long long t = A[i].first.first;
    long long c = A[i].second;
    L[t] = min(L[t], L[s] + c);
    while (L[t - 1] == 1000000000000000000) {
      L[t - 1] = L[t];
      t--;
    }
  }
  if (L[N - 1] >= 1000000000000000000) {
    cout << -1 << endl;
    return 0;
  }
  cout << L[N - 1] << endl;
  return 0;
}
