#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
long long get_min(vector<vector<long long>>& rmq, int l, int r) {
  if (l == r) return rmq[0][l];
  long long len = 1;
  int t = 0;
  while (2 * len < r - l + 1) {
    len <<= 1;
    t++;
    rmq[t][l] = min(rmq[t - 1][l], rmq[t - 1][l + len / 2]);
    rmq[t][r - len + 1] =
        min(rmq[t - 1][r - len + 1], rmq[t - 1][r - len + 1 + len / 2]);
  }
  return min(rmq[t][l], rmq[t][r - len + 1]);
}
void update(vector<vector<long long>>& rmq, int pos, long long newmin) {
  rmq[0][pos] = min(rmq[0][pos], newmin);
  long long len = 1;
  for (int(t) = 1; (t) <= (19); (t)++) {
    if (pos + len >= rmq[t - 1].size()) break;
    rmq[t][pos] = min(rmq[t - 1][pos], rmq[t - 1][pos + len]);
    len <<= 1;
  }
}
int main(int argc, char** argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789) {
    freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
  }
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> a(m);
  for (int(i) = 0; (i) < (m); (i)++) {
    int l, r, c;
    cin >> l >> r >> c;
    a[i] = make_tuple(l, r, c);
  }
  sort(a.begin(), a.end());
  vector<vector<long long>> rmq(20, vector<long long>(n + 1, INF));
  update(rmq, 1, 0);
  for (int(i) = 0; (i) < (m); (i)++) {
    int l = get<0>(a[i]);
    int r = get<1>(a[i]);
    long long d = get<2>(a[i]);
    long long mindist = get_min(rmq, l, r - 1);
    long long d2 = d + mindist;
    update(rmq, r, d2);
  }
  long long dn = get_min(rmq, n, n);
  if (dn == INF)
    cout << -1 << endl;
  else
    cout << dn << endl;
  if (argc == 2 && atoi(argv[1]) == 123456789)
    cerr << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
  return 0;
}
