#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, l, r, c;
  long long cost[100000 + 1] = {0};
  tuple<int, int, int> edges[100000 + 1];
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> l >> r >> c;
    edges[i] = make_tuple(r, l, c);
  }
  sort(edges, edges + M);
  for (int i = 2; i <= N; i++) {
    cost[i] = (1e9 * (100000 + 1));
    for (int j = 0; (i > get<1>(edges[j]) || i <= get<0>(edges[j])) && j < M;
         j++)
      if (i > get<1>(edges[j]) && i <= get<0>(edges[j]))
        cost[i] = min(cost[get<1>(edges[j])] + get<2>(edges[j]), cost[i]);
  }
  if (cost[N] == (1e9 * (100000 + 1)))
    cout << -1 << endl;
  else
    cout << cost[N] << endl;
}
