#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
struct edge {
  long long to, cost;
};
long long V;
vector<edge> G[200000];
long long d[200000];
void dijkstra(int S) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  fill(d, d + V, (1LL << 56));
  d[S] = 0;
  que.push(pair<int, int>(0, S));
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) continue;
    for (int(i) = 0; (i) < (G[v].size()); (i)++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }
}
int main(void) {
  long long M;
  cin >> V >> M;
  for (int(i) = 0; (i) < (V - 1); (i)++) {
    edge in;
    in.to = i, in.cost = 0;
    G[i + 1].push_back(in);
  }
  for (int(i) = 0; (i) < (M); (i)++) {
    edge in;
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--;
    in.to = y, in.cost = c;
    G[x].push_back(in);
  }
  dijkstra(0);
  if (d[V - 1] == (1LL << 56))
    cout << -1 << "\n";
  else
    cout << d[V - 1] << "\n";
}
