#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const int MOD = 1e9 + 7;
const int INF = numeric_limits<int>::max();
const ll INF_MAX = numeric_limits<ll>::max();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vvi df(m, vi(3));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> df[i][j];
    }
  }
  sort(df.begin(), df.end());
  map<int, ll> shortest;
  shortest[1] = 0;
  shortest[n] = INF_MAX;
  for (auto row : df) {
    int left = row[0];
    int right = row[1];
    int distance = row[2];
    auto begin = shortest.lower_bound(left);
    auto last = shortest.lower_bound(right);
    if (begin->second == INF_MAX) continue;
    ll length = begin->second + distance;
    if (length < last->second) {
      shortest[right] = length;
    }
  }
  if (shortest[n] == INF_MAX) {
    cout << -1 << endl;
  } else {
    cout << shortest[n] << endl;
  }
}
