#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T>
using Vvec = vector<vector<T> >;
class Edge {
 public:
  ll L, R, C;
  Edge() : L(0), R(0), C(0) {}
  Edge(ll L, ll R, ll C) : L(L), R(R), C(C) {}
};
bool cmp(Edge a, Edge b) {
  if (a.R == b.R) return a.C < b.C;
  return a.R < b.R;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<Edge> graph(M);
  for (int i = 0; i < M; i++) {
    cin >> graph[i].L >> graph[i].R >> graph[i].C;
    graph[i].L--;
    graph[i].R--;
  }
  sort(graph.begin(), graph.end(), cmp);
  vector<ll> dist(N, 1e18);
  dist[0] = 0;
  int j = 0;
  for (int i = 1; i < N; i++) {
    while (graph[j].R < i) {
      j++;
      if (j >= M) {
        cout << -1 << endl;
        return 0;
      }
    }
    dist[i] = min(dist[i], dist[graph[j].L] + graph[j].C);
    if (j != N - 1 && graph[j].R == graph[j + 1].R) {
      j++;
      i--;
    }
  }
  if (dist[N - 1] > 1e17) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[N - 1] << endl;
}
