#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
int G[N * 10], to[N * 100], w[N * 100], nxt[N * 100], sz = 0, tot, id1[N << 2],
                                                      id2[N << 2], x, y, p, c;
long long d[N * 10];
struct node {
  int p, d;
  node() {}
  node(int p, int d) : p(p), d(d) {}
  inline bool operator<(const node &b) const { return d > b.d; }
};
priority_queue<node> Q;
inline void adde(int u, int v, int c) {
  to[++sz] = v;
  w[sz] = c;
  nxt[sz] = G[u];
  G[u] = sz;
}
void build(int o, int L, int R) {
  if (L == R) {
    adde(L, id1[o] = ++tot, 0);
    adde(id2[o] = ++tot, L, 0);
  } else {
    int lc = o << 1, rc = lc | 1, M = L + R >> 1;
    build(lc, L, M);
    build(rc, M + 1, R);
    adde(id1[lc], id1[o] = ++tot, 0);
    adde(id1[rc], id1[o], 0);
    adde(id2[o] = ++tot, id2[lc], 0);
    adde(id2[o], id2[rc], 0);
  }
}
void ask(int o, int L, int R) {
  if (x <= L && y >= R) {
    adde(id1[o], p, c);
    adde(p, id2[o], 0);
  } else {
    int lc = o << 1, rc = lc | 1, M = L + R >> 1;
    if (x <= M) ask(lc, L, M);
    if (y > M) ask(rc, M + 1, R);
  }
}
int main() {
  int n, m, i, u, v;
  node h;
  scanf("%d%d", &n, &m);
  build(1, 1, tot = n);
  while (m--) {
    scanf("%d%d%d", &x, &y, &c);
    p = ++tot;
    ask(1, 1, n);
  }
  memset(d, 0x3f, sizeof(d));
  Q.push(node(1, d[1] = 0ll));
  while (!Q.empty()) {
    h = Q.top();
    Q.pop();
    if (d[u = h.p] != h.d) continue;
    if (u == n) {
      printf("%lld\n", d[n]);
      return 0;
    }
    for (i = G[u]; i; i = nxt[i])
      if (d[u] + w[i] < d[v = to[i]]) Q.push(node(v, d[v] = d[u] + w[i]));
  }
  puts("-1");
  return 0;
}
