#include <bits/stdc++.h>
struct ios {
  template <typename TP>
  inline ios operator>>(TP &x) {
    TP f = 1;
    x = 0;
    register char c = getchar();
    for (; c > '9' || c < '0'; c = getchar())
      if (c == '-') f = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
      x = (x << 3) + (x << 1) + (c ^ '0');
    x *= f;
    return *this;
  }
  template <typename TP>
  inline ios operator<<(TP x) {
    int top = 0, s[66];
    if (x < 0) x = -x, putchar('-');
    if (!x) putchar('0');
    while (x) s[++top] = x % 10 + '0', x /= 10;
    while (top) putchar(s[top--]);
    return *this;
  }
  inline ios operator<<(char s) {
    putchar(s);
    return *this;
  }
} io;
const int N = 1e5 + 5;
int n, m;
const int inf = 1 << 30;
struct node {
  int l, r, val;
  inline bool operator<(const node &b) const {
    return l < b.l || (l == b.l && r < b.r);
  }
} seq[N];
int Mn[N << 2], lazy[N << 2];
inline void pushup(int rt) { Mn[rt] = std::min(Mn[rt << 1], Mn[rt << 1 | 1]); }
inline void build(int l, int r, int rt) {
  Mn[rt] = lazy[rt] = inf;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
  pushup(rt);
}
inline void pushdown(int rt) {
  if (lazy[rt] != inf) {
    if (lazy[rt] < lazy[rt << 1]) {
      Mn[rt << 1] = std::min(Mn[rt << 1], lazy[rt]);
      lazy[rt << 1] = lazy[rt];
    }
    if (lazy[rt] < lazy[rt << 1 | 1]) {
      Mn[rt << 1 | 1] = std::min(Mn[rt << 1 | 1], lazy[rt]);
      lazy[rt << 1 | 1] = lazy[rt];
    }
    lazy[rt] = inf;
  }
}
inline void update(int L, int R, int val, int l, int r, int rt) {
  if (L <= l && r <= R) {
    lazy[rt] = std::min(lazy[rt], val);
    Mn[rt] = std::min(Mn[rt], val);
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(rt);
  if (L <= mid) update(L, R, val, l, mid, rt << 1);
  if (R > mid) update(L, R, val, mid + 1, r, rt << 1 | 1);
  pushup(rt);
}
inline int query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return Mn[rt];
  int mid = (l + r) >> 1, ret = inf;
  pushdown(rt);
  if (L <= mid) ret = std::min(ret, query(L, R, l, mid, rt << 1));
  if (R > mid) ret = std::min(ret, query(L, R, mid + 1, r, rt << 1 | 1));
  return ret;
}
signed main() {
  io >> n >> m;
  for (register int i = 1; i <= m; ++i)
    io >> seq[i].l >> seq[i].r >> seq[i].val;
  std::sort(seq + 1, seq + m + 1);
  int cnt = 0;
  build(1, n, 1), update(1, 1, 0, 1, n, 1);
  for (register int i = 1; i <= n; ++i) {
    int nw = query(i, i, 1, n, 1);
    while (cnt < m && seq[cnt + 1].l <= i) {
      ++cnt;
      if (i >= seq[cnt].l && i < seq[cnt].r)
        update(i + 1, seq[cnt].r, nw + seq[cnt].val, 1, n, 1);
    }
  }
  int ans = query(n, n, 1, n, 1);
  if (ans == inf)
    puts("-1");
  else
    io << ans;
  return 0;
}
