#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const long long INF = 1e18 + 8;
int n, m;
set<int> sp;
struct node {
  int L, R;
  long long C;
  node(int L = 0, int R = 0, long long C = 0) : L(L), R(R), C(C) {}
  void scan() { scanf("%d%d%lld", &L, &R, &C); }
  bool operator<(const node x) const {
    if (L != x.L) return L < x.L;
    if (R != x.R) return R < x.R;
    return C > x.C;
  }
} v[N];
struct point {
  int x, y;
  long long d;
  point(int x = 0, int y = 0, long long d = 0) : x(x), y(y), d(d) {}
  bool operator<(const point b) const { return d < b.d; }
};
long long dis[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) dis[i] = INF;
  for (int i = 0; i < m; i++) {
    v[i].scan();
  }
  sort(v, v + m);
  priority_queue<point> pq;
  point it = point(1, 1, 0ll);
  dis[1] = 0;
  pq.push(it);
  while (!pq.empty()) {
    it = pq.top();
    pq.pop();
    for (int i = lower_bound(v, v + m, node(it.x, it.y, it.d)) - v;
         i < m && v[i].L <= it.y; i++) {
      if (dis[v[i].R] > v[i].C + it.d) {
        dis[v[i].R] = v[i].C + it.d;
        pq.push(point(v[i].L, v[i].R, dis[v[i].R]));
      }
    }
  }
  if (dis[n] == INF) dis[n] = -1;
  printf("%lld\n", dis[n]);
}
