#include <bits/stdc++.h>
using namespace std;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
template <typename T>
constexpr T inf = numeric_limits<T>::has_infinity
                      ? numeric_limits<T>::infinity()
                      : numeric_limits<T>::max() / 4;
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& a) {
  return is >> a.first >> a.second;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& a) {
  for (T& x : a) is >> x;
  return is;
}
void LOG(const int& x) { cerr << x << "\n"; }
template <typename T>
void LOG(const vector<T>& a) {
  for (const T& x : a) cerr << x << " ";
  cerr << "\n";
}
template <typename T, std::size_t S>
void LOG(const T (&a)[S]) {
  for (int i = 0, i_len = S; i < i_len; ++i) cerr << a[i] << " ";
  cerr << "\n";
}
template <typename T, std::size_t S, std::size_t R>
void LOG(const T (&a)[S][R]) {
  for (int i = 0, i_len = S; i < i_len; ++i) {
    for (int j = 0, j_len = R; j < j_len; ++j) cerr << a[i][j] << " ";
    cerr << "\n";
  }
}
i64 ceil(i64 a, i64 b) { return (a - 1) / b + 1; }
i64 gcd(i64 a, i64 b) {
  while (b != 0) {
    i64 t = b;
    b = a % b;
    a = t;
  }
  return a;
}
i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
i64 gcd(const vector<i64>& v) {
  return accumulate((v).begin(), (v).end(), 1LL,
                    static_cast<i64 (*)(i64, i64)>(gcd));
}
i64 lcm(const vector<i64>& v) {
  return v.empty() ? 0LL
                   : accumulate(v.begin() + 1, v.end(), v[0],
                                static_cast<i64 (*)(i64, i64)>(lcm));
}
i64 pow(i64 x, i64 y, i64 z) {
  i64 a = 1;
  while (y > 0) {
    if (y & 1) a = a * x % z;
    x = x * x % z;
    y /= 2;
  }
  return a;
}
i64 inv(i64 x, i64 m) { return pow(x, m - 2, m); }
struct InitCpp {
  InitCpp() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} initCpp;
const i64 INF = inf<i64>;
using P = pair<i64, i64>;
struct E {
  i64 to;
  i64 cost;
};
signed main() {
  i64 N, M;
  cin >> N >> M;
  vector<vector<E>> G(N);
  for (int _ = 0, __len = N; _ < __len; ++_) {
    i64 l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back({r, c});
  }
  for (int i = 1, i_len = N; i < i_len; ++i) {
    G[i].push_back({i - 1, 0});
  }
  priority_queue<P, vector<P>, greater<P>> que;
  vector<i64> D(N, INF);
  D[1] = 0;
  que.push({0, 1});
  while (que.size()) {
    P p = que.top();
    que.pop();
    if (D[p.second] < p.first) continue;
    for (auto e : G[p.second]) {
      if (D[e.to] > D[p.second] + e.cost) {
        D[e.to] = D[p.second] + e.cost;
        que.push({D[e.to], e.to});
      }
    }
  }
  cout << (D[N - 1] == INF ? -1 : D[N - 1]) << endl;
}
