#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int M;
  cin >> N;
  cin >> M;
  vector<tuple<int, int, long long>> V(M);
  for (int i = 0; i < M; i++) {
    int L;
    int R;
    long long C;
    cin >> L;
    cin >> R;
    cin >> C;
    tuple<int, int, long long> T = make_tuple(R - 1, L - 1, C);
    V[i] = T;
  }
  vector<long long> dist(N);
  for (int i = 0; i < N; i++) {
    dist[i] = 10000000000000;
  }
  dist[0] = 0;
  sort(V.begin(), V.end());
  set<int> S;
  S.insert(0);
  for (int i = 0; i < M; i++) {
    int y = get<1>(V[i]);
    auto x = S.lower_bound(y);
    int X = *x;
    dist[get<0>(V[i])] = min(dist[get<0>(V[i])], dist[X] + get<2>(V[i]));
    S.insert(get<0>(V[i]));
  }
  if (dist[N - 1] == 10000000000000) {
    cout << -1 << endl;
  } else {
    cout << dist[N - 1] << endl;
  }
}
