#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int to;
  long cost;
} edge;
typedef struct {
  long dist;
  int v;
} vtx;
bool operator<(const vtx a, const vtx b) { return a.dist > b.dist; }
bool operator>(const vtx a, const vtx b) { return a.dist < b.dist; }
long distance(int s, int g, vector<edge>* G, int N) {
  long d[N];
  fill(d, d + N, 1e15);
  priority_queue<vtx> que;
  d[s] = 0;
  que.push(vtx{d[s], s});
  while (!que.empty()) {
    vtx now = que.top();
    que.pop();
    if (now.dist > d[now.v]) continue;
    for (edge e : G[now.v]) {
      if (d[e.to] > d[now.v] + e.cost) {
        d[e.to] = d[now.v] + e.cost;
        que.push(vtx{d[e.to], e.to});
      }
    }
  }
  if (d[g] == 1e15)
    return -1;
  else
    return d[g];
}
int main() {
  int N, M;
  cin >> N >> M;
  int l, r;
  long c;
  vector<edge> G[N];
  for (int i = 0; i < M; i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].emplace_back(edge{r, c});
    G[r].emplace_back(edge{l, c});
    for (int j = l; j < r - 1; j++) {
      G[j + 1].emplace_back(edge{j, 0});
    }
  }
  cout << distance(0, N - 1, G, N) << endl;
  return 0;
}
