#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long INF = 1e16;
struct P {
  int l, r, c;
  bool operator<(const P& b) const {
    if (l != b.l) return l < b.l;
    if (r != b.r) return r < b.r;
    return c < b.c;
  }
} p[maxn];
int n, m;
long long seg[maxn << 2], tg[maxn << 2];
void push_down(int o) {
  if (tg[o] == -1) return;
  seg[o << 1] = seg[o << 1 | 1] = tg[o];
  tg[o << 1] = tg[o << 1 | 1] = tg[o];
  tg[o] = -1;
}
void push_up(int o) { seg[o] = min(seg[o << 1], seg[o << 1 | 1]); }
void build(int l = 1, int r = n, int o = 1) {
  tg[o] = -1;
  if (l == r) {
    seg[o] = INF;
    return;
  }
  int m = (l + r) >> 1;
  build(l, m, o << 1), build(m + 1, r, o << 1 | 1);
  push_up(o);
}
void update(int ql, int qr, long long k, int l = 1, int r = n, int o = 1) {
  if (ql <= l && r <= qr) {
    seg[o] = tg[o] = k;
    return;
  }
  push_down(o);
  int m = (l + r) >> 1;
  if (ql <= m) update(ql, qr, k, l, m, o << 1);
  if (qr > m) update(ql, qr, k, m + 1, r, o << 1 | 1);
  push_up(o);
}
long long query(int ql, int qr, int l = 1, int r = n, int o = 1) {
  if (ql <= l && r <= qr) return seg[o];
  push_down(o);
  int m = (l + r) >> 1;
  if (qr <= m) return query(ql, qr, l, m, o << 1);
  if (ql > m) return query(ql, qr, m + 1, r, o << 1 | 1);
  return min(query(ql, qr, l, m, o << 1), query(ql, qr, m + 1, r, o << 1 | 1));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d%d%d", &p[i].l, &p[i].r, &p[i].c);
  sort(p, p + m);
  if (p[0].l != 1) return puts("-1"), 0;
  int cur = 1;
  build();
  update(1, 1, 0);
  for (int i = 0; i < m; i++) {
    long long x = query(p[i].l, p[i].r);
    if (x == INF) return puts("-1"), 0;
    long long now = query(p[i].r, n);
    if (now > x + p[i].c) update(p[i].r, p[i].r, x + p[i].c);
    if (cur < p[i].r) cur = p[i].r;
  }
  long long ans = query(n, n);
  if (ans == INF)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
