#include <bits/stdc++.h>
using namespace std;
inline void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
inline void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
inline void Yay(bool b) { cout << (b ? "Yay!" : ":(") << endl; }
const long long INF = 1e16;
struct edge {
  int f, t;
  long long c;
};
bool comp(const edge& l, const edge& r) {
  if (l.t == r.t) {
    if (l.c == r.c) return l.f < r.f;
    return l.c < r.c;
  }
  return l.t > r.t;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(15);
  int N, M;
  cin >> N >> M;
  vector<edge> v(M);
  vector<edge> w(M);
  for (int i = 0, i_max = (M); i < i_max; ++i)
    cin >> v[i].f >> v[i].t >> v[i].c;
  sort((v).begin(), (v).end(), comp);
  vector<long long> cost(N + 1, INF);
  cost[N] = 0;
  int index = 0;
  for (int x = N + 1 - 1, x_min(1); x >= x_min; --x) {
    if (cost[x] == INF) continue;
    while (index < N && v[index].t >= x) {
      if (v[index].f > x) {
        ++index;
        continue;
      }
      cost[v[index].f] = min(cost[v[index].f], cost[x] + v[index].c);
      ++index;
    }
  }
  if (cost[1] == INF)
    cout << -1 << endl;
  else
    cout << cost[1] << endl;
  return 0;
}
