#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const int N = 1000000;
int w[N], W[N], lewa[N], prawa[N], M, a[N], b[N], c[N], i, kol[N], n, m, ww, l;
bool cmp(int q1, int q2) { return a[q1] < a[q2]; }
void insert(int a, int b, int c, int d = 1) {
  if (lewa[d] > b || prawa[d] < a) return;
  if (lewa[d] == a && prawa[d] == b) {
    w[d] = min(w[d], c);
    W[d] = w[d];
    if (d <= M) W[d] = min(W[d], min(W[2 * d], W[2 * d + 1]));
    return;
  }
  insert(a, min(b, prawa[2 * d]), c, 2 * d);
  insert(max(a, lewa[2 * d + 1]), b, c, 2 * d + 1);
  W[d] = min(w[d], min(W[2 * d], W[2 * d + 1]));
}
int query(int a, int b, int d = 1) {
  if (lewa[d] > b || prawa[d] < a) return INF;
  if (lewa[d] == a && prawa[d] == b) return W[d];
  return min(w[d], min(query(a, min(b, prawa[2 * d]), 2 * d),
                       query(max(a, lewa[2 * d + 1]), b, 2 * d + 1)));
}
void init(int a) {
  int i;
  M = 1;
  while (M < a) M *= 2;
  for (i = M; i < 2 * M; i++) {
    w[i] = W[i] = INF;
    lewa[i] = prawa[i] = i - M + 1;
  }
  for (i = M - 1; i >= 1; i--) {
    w[i] = W[i] = INF;
    lewa[i] = lewa[2 * i];
    prawa[i] = prawa[2 * i + 1];
  }
  M--;
}
int main() {
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    kol[i] = i;
  }
  sort(kol, kol + m, cmp);
  init(n);
  insert(1, 1, 0);
  for (i = 0; i < m; i++) {
    ww = kol[i];
    l = query(a[ww], a[ww]);
    if (l == INF) {
      printf("-1\n");
      return 0;
    }
    insert(a[ww], b[ww], l + c[ww]);
  }
  l = query(n, n);
  if (l == INF) l = -1;
  printf("%d\n", l);
  return 0;
}
