#include <bits/stdc++.h>
using namespace std;
int n, m, l, r, c;
long long t[400009], inf = 2e14;
vector<pair<int, pair<int, int> > > v;
void update(int v, int s, int e, int pos, long long val) {
  if (s == e) {
    t[v] = min(t[v], val);
    return;
  }
  int m = (s + e) / 2;
  pos <= m ? update(2 * v, s, m, pos, val)
           : update(2 * v + 1, m + 1, e, pos, val);
  t[v] = min(t[2 * v], t[2 * v + 1]);
}
long long solve(int v, int s, int e, int l, int r) {
  if (s > r || e < l) return inf;
  if (s >= l && e <= r) return t[v];
  int m = (s + e) / 2;
  return min(solve(2 * v, s, m, l, r), solve(2 * v + 1, m + 1, e, l, r));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  while (m--) {
    cin >> l >> r >> c;
    v.push_back({l, {r, c}});
  }
  for (int i = 1; i <= 4 * n; i++) t[i] = inf;
  sort(v.begin(), v.end());
  if (v[0].first != 1) return cout << -1, 0;
  for (auto i : v) {
    if (i.first == 1) {
      update(1, 1, n, i.second.first, i.second.second);
      continue;
    }
    long long x = solve(1, 1, n, i.first, n);
    update(1, 1, n, i.second.first, i.second.second + x);
  }
  long long x = solve(1, 1, n, n, n);
  if (x >= inf) x = -1;
  cout << x;
  return 0;
}
