#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, M;
  cin >> N >> M;
  vector<tuple<int, long long, int>> R;
  int amax = 0;
  for (int i = 0; i < N; i++) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    R.emplace_back(a - 1, c, b - 1);
  }
  sort(R.begin(), R.end());
  vector<long long> Tb(N, -1);
  Tb[0] = 0;
  for (int i = 0; i < R.size(); i++) {
    int a = get<0>(R[i]);
    long long c = get<1>(R[i]);
    int b = get<2>(R[i]);
    if (Tb[a] == -1) {
      cout << -1 << endl;
      return 0;
    }
    for (int j = a + 1; j <= b; j++) {
      if (Tb[j] == -1 || Tb[j] > Tb[a] + c) {
        Tb[j] = Tb[a] + c;
      }
    }
  }
  cout << Tb[N - 1] << endl;
  return 0;
}
