#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> G(N);
  for (int i = 0; i < N - 1; i++) {
    G[i + 1].emplace_back(make_pair(i, 0));
  }
  for (int i = 0; i < M; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    G[l].emplace_back(make_pair(r, c));
  }
  priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> que;
  que.push(make_pair(0, 0));
  vector<int64_t> dist(N, INF);
  dist[0] = 0;
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int u = q.second;
    for (auto g : G[u]) {
      int v = g.first, c = g.second;
      if (chmin(dist[v], dist[u] + c)) {
        que.push(make_pair(dist[v], v));
      }
    }
  }
  if (dist[N - 1] != INF) {
    cout << dist[N - 1] << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}
