#include <bits/stdc++.h>
using namespace std;
long long lim = 100005;
long long t[4 * 100005];
void update(long long v, long long tl, long long tr, long long l, long long r,
            long long add) {
  if (l > r) return;
  if (l == tl && r == tr) {
    t[v] = add;
  } else {
    long long tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), add);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add);
  }
}
long long get(long long v, long long tl, long long tr, long long pos) {
  if (tl == tr) return t[v];
  long long tm = (tl + tr) / 2;
  if (pos <= tm)
    return t[v] + get(v * 2, tl, tm, pos);
  else
    return t[v] + get(v * 2 + 1, tm + 1, tr, pos);
}
struct cont {
  long long lft;
  long long rgt;
  long long dist;
};
struct cmp {
  bool operator()(const cont& c, const cont& d) const {
    if (c.dist != d.dist) {
      return c.dist < d.dist;
    } else {
      return make_pair(c.lft, c.rgt) < make_pair(d.lft, d.rgt);
    }
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (long long i = 0; i < lim * 4; ++i) {
    t[i] = 0;
  }
  long long n, m;
  cin >> n >> m;
  vector<cont> ranges(m);
  map<long long, vector<cont>> mp;
  for (long long i = 0; i < m; ++i) {
    cin >> ranges[i].lft >> ranges[i].rgt >> ranges[i].dist;
    mp[ranges[i].lft].push_back(ranges[i]);
  }
  long long pres = 1;
  set<cont, cmp> st;
  for (auto it : mp[pres]) {
    st.insert(it);
  }
  while (!st.empty()) {
    auto it = *st.begin();
    st.erase(it);
    long long lft = it.lft, rgt = it.rgt, cost = it.dist;
    if (rgt > pres) {
      long long s = pres + 1, e = it.rgt;
      for (long long tt = s; tt <= e; ++tt) {
        for (auto it : mp[tt]) {
          st.insert(it);
        }
      }
      pres = e;
      long long cst = get(1, 1, n, it.lft) + cost;
      update(1, 1, n, s, e, cst);
    }
  }
  if (pres < n) {
    cout << -1;
    return 0;
  }
  cout << get(1, 1, n, n);
}
