#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 7, INF = 5e18;
bool live[N << 2];
long long prop[N << 2], mn[N << 2];
void build(long long v, long long tl, long long tr) {
  mn[v] = INF;
  prop[v] = INF;
  live[v] = 1;
  if (tl == tr) return;
  long long tm = (tl + tr) >> 1;
  build(v * 2 + 1, tl, tm);
  build(v * 2 + 2, tm + 1, tr);
}
void gap(long long v, long long x) {
  if (!live[v]) return;
  prop[v] = min(prop[v], x);
  mn[v] = min(mn[v], x);
}
void push(long long v) {
  if (prop[v] != INF) {
    gap(v * 2 + 1, prop[v]);
    gap(v * 2 + 2, prop[v]);
    prop[v] = INF;
  }
}
void relax(long long v) {
  live[v] = live[v * 2 + 1] || live[v * 2 + 2];
  mn[v] = min(mn[v * 2 + 1], mn[v * 2 + 2]);
}
void upd(long long v, long long tl, long long tr, long long l, long long r,
         long long x) {
  if (tr < l || r < tl) return;
  if (l <= tl && tr <= r) {
    gap(v, x);
    return;
  }
  long long tm = (tl + tr) >> 1;
  push(v);
  upd(v * 2 + 1, tl, tm, l, r, x);
  upd(v * 2 + 2, tm + 1, tr, l, r, x);
  relax(v);
}
pair<long long, long long> get(long long v, long long tl, long long tr) {
  if (mn[v] == INF || !live[v]) {
    return make_pair(-1, -1);
  }
  if (tl == tr) {
    live[v] = 0;
    long long ret = mn[v];
    mn[v] = INF;
    return make_pair(tl, ret);
  }
  long long tm = (tl + tr) >> 1;
  push(v);
  if (mn[v * 2 + 1] == mn[v]) {
    auto t = get(v * 2 + 1, tl, tm);
    relax(v);
    if (t.first != -1) return t;
  }
  if (mn[v * 2 + 2] == mn[v]) {
    auto t = get(v * 2 + 2, tm + 1, tr);
    relax(v);
    if (t.first != -1) return t;
  }
  return make_pair(-1, -1);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> l(m), r(m), c(m);
  vector<bool> used(m);
  for (long long i = 0; i < m; ++i) {
    cin >> l[i] >> r[i] >> c[i];
  }
  build(0, 1, n);
  upd(0, 1, n, 1, 1, 0);
  while (1) {
    auto t = get(0, 1, n);
    if (t.first == -1) break;
    if (t.first == n) {
      cout << t.second << '\n';
      exit(0);
    }
    long long u = t.first, d = t.second;
    for (long long i = 0; i < m; ++i) {
      if (!used[i] && l[i] <= u && u <= r[i]) {
        upd(0, 1, n, l[i], r[i], d + c[i]);
        used[i] = 1;
      }
    }
  }
  cout << -1 << '\n';
}
