#include <bits/stdc++.h>
using namespace std;
const int RAGETREE_DEPTH = 17;
const int RAGETREE_SZ = 1 << RAGETREE_DEPTH;
long long ragetree[2 * RAGETREE_SZ];
long long delay[2 * RAGETREE_SZ];
void apply(int idx, long long val) {
  ragetree[idx] = min(ragetree[idx], val);
  if (delay[idx] > 0 && delay[idx] < val) return;
  delay[idx] = val;
}
void build(int idx) {
  assert(idx >= RAGETREE_SZ);
  while (idx > 1) {
    idx /= 2;
    ragetree[idx] = min(ragetree[2 * idx], ragetree[2 * idx + 1]);
  }
}
void push(int idx) {
  assert(idx >= RAGETREE_SZ);
  for (int d = RAGETREE_DEPTH; d > 0; d--) {
    int i = idx >> d;
    if (delay[i]) {
      apply(2 * i, delay[i]);
      apply(2 * i + 1, delay[i]);
      delay[i] = 0;
    }
  }
}
void upd(int lhs, int rhs, long long val) {
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  int ol = lhs;
  int orr = rhs;
  while (lhs <= rhs) {
    if (lhs == rhs) {
      apply(lhs, val);
      break;
    }
    if (lhs % 2) apply(lhs++, val);
    if (rhs % 2 == 0) apply(rhs--, val);
    lhs /= 2;
    rhs /= 2;
  }
  build(ol);
  build(orr);
}
long long qry(int lhs, int rhs) {
  long long ret = 1e18;
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  push(lhs);
  push(rhs);
  while (lhs <= rhs) {
    if (lhs == rhs) {
      ret = min(ret, ragetree[lhs]);
      break;
    }
    if (lhs % 2) ret = min(ret, ragetree[lhs++]);
    if (rhs % 2 == 0) ret = min(ret, ragetree[rhs--]);
    lhs /= 2;
    rhs /= 2;
  }
  return ret;
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 2 * RAGETREE_SZ; i++) ragetree[i] = 1e18;
  upd(1, 1, 0);
  vector<pair<pair<int, int>, int> > v;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v.push_back({pair<int, int>(a, b), c});
  }
  sort(v.begin(), v.end());
  for (auto out : v) {
    long long amt = qry(out.first.first, out.first.first);
    upd(out.first.first + 1, out.first.second, amt + out.second);
  }
  long long ret = qry(n, n);
  if (ret >= 1e18) ret = -1;
  cout << ret << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
