#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int mn[maxn << 3], lazy[maxn << 3];
void up(int t) { mn[t] = min(mn[t << 1], mn[t << 1 | 1]); }
void down(int t) {
  if (lazy[t]) {
    mn[t << 1] = min(mn[t << 1], lazy[t]);
    mn[t << 1 | 1] = min(mn[t << 1 | 1], lazy[t]);
    lazy[t << 1] = min(lazy[t << 1], lazy[t]);
    lazy[t << 1 | 1] = min(lazy[t << 1 | 1], lazy[t]);
    lazy[t] = 0;
  }
}
void build(int l, int r, int t) {
  if (l == r) {
    mn[t] = (1 << 30);
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, t << 1);
  build(mid + 1, r, t << 1 | 1);
  up(t);
}
void update(int ll, int rr, int l, int r, int num, int t) {
  if (ll > rr) return;
  if (ll <= l && r <= rr) {
    mn[t] = min(mn[t], num);
    if (lazy[t])
      lazy[t] = min(num, lazy[t]);
    else
      lazy[t] = num;
    return;
  }
  int mid = (l + r) >> 1;
  down(t);
  if (ll <= mid) update(ll, rr, l, mid, num, t << 1);
  if (rr > mid) update(ll, rr, mid + 1, r, num, t << 1 | 1);
  up(t);
}
int solve(int pos, int l, int r, int t) {
  if (l == r) {
    return mn[t];
  }
  int mid = (l + r) >> 1;
  down(t);
  if (pos <= mid)
    return solve(pos, l, mid, t << 1);
  else
    return solve(pos, mid + 1, r, t << 1 | 1);
}
struct query {
  int l, r, w;
  bool operator<(const query &h) const { return l == h.l ? r < h.r : l < h.l; }
} e[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  build(1, n, 1);
  for (int i = 1; i <= m; ++i) {
    cin >> e[i].l >> e[i].r >> e[i].w;
  }
  sort(e + 1, e + m + 1);
  update(1, 1, 1, n, 0, 1);
  for (int i = 1; i <= m; ++i) {
    int tmp = solve(e[i].l, 1, n, 1);
    update(e[i].l + 1, e[i].r, 1, n, tmp + e[i].w, 1);
  }
  int tmp = solve(n, 1, n, 1);
  cout << (tmp == (1 << 30) ? -1 : tmp) << '\n';
  return 0;
}
