#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
const int inf = (int)1e9 + 7;
const long long INF = (long long)1e18;
int n, m;
int l[N], r[N], c[N];
long long de[N];
vector<int> event[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> l[i] >> r[i] >> c[i];
    event[l[i]].push_back(i);
    event[r[i] + 1].push_back(-i);
  }
  set<long long> cur;
  for (int j = 1; j <= n; ++j) {
    for (int i : event[j]) {
      if (i < 0) {
        cur.erase(cur.find(min(INF, de[-i] + c[-i])));
      }
    }
    for (int i : event[j]) {
      if (i > 0) {
        if (cur.empty() || l[i] == 1) {
          if (l[i] == 1) {
            de[i] = 0;
          } else {
            de[i] = INF;
          }
        } else {
          de[i] = *cur.begin();
        }
        cur.insert(min(INF, de[i] + c[i]));
      }
    }
  }
  long long ans = INF;
  for (int i = 1; i <= m; ++i) {
    if (r[i] == n) {
      ans = min(ans, de[i] + c[i]);
    }
  }
  if (ans == INF) ans = -1;
  cout << ans;
  return 0;
}
