#include <bits/stdc++.h>
using namespace std;
int n, m;
int MAXIMUM = 100000;
long INF = 1e18;
int W = 0, G = 1, B = 2;
vector<pair<long, long>> adj[100000];
int dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int u, minv;
  long d[MAXIMUM], color[MAXIMUM];
  for (int i = 0; i < n; i++) {
    d[i] = INF;
    color[i] = W;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = G;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;
    color[u] = B;
    if (d[u] < f.first * (-1)) continue;
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == B) continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = G;
      }
    }
  }
  if (d[n - 1] != INF)
    cout << d[n - 1] << endl;
  else
    cout << -1 << endl;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    adj[l - 1].push_back(make_pair(r - 1, c));
  }
  for (int i = 1; i < n; i++) {
    adj[i].push_back(make_pair(i - 1, 0));
  }
  dijkstra();
}
