#include <bits/stdc++.h>
using namespace std;
template <class... Args>
string dbgFormat(const char* fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}
template <class Head>
void dbgLog(Head&& head) {
  cerr << head << endl;
}
template <class Head, class... Tail>
void dbgLog(Head&& head, Tail&&... tail) {
  cerr << head << " ";
  dbgLog(forward<Tail>(tail)...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << ']';
  return os;
}
template <typename T, typename T2, typename T3>
ostream& operator<<(ostream& os, const priority_queue<T, T2, T3>& orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first) os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}
bool operator<(const pair<long long int, long long int> p1,
               const pair<long long int, long long int> p2) {
  if (p1.first < p2.first) return true;
  if (p1.first > p2.first) return false;
  return p1.second < p2.second;
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);
  long long int N, M;
  cin >> N >> M;
  vector<long long int> L(M), R(M), C(M);
  for (int i = 0; i < M; i++) {
    cin >> L.at(i) >> R.at(i) >> C.at(i);
  }
  vector<long long int> ordL(M);
  iota(ordL.begin(), ordL.end(), 0);
  sort(ordL.begin(), ordL.end(),
       [&](int p, int q) { return L.at(p) < L.at(q); });
  set<pair<long long int, long long int> > theS;
  theS.emplace(1, 0);
  for (int i = 0; i < M; i++) {
    long long int j = ordL.at(i);
    ;
    auto it = theS.lower_bound(make_pair(L.at(j), 0));
    if (it == theS.end()) {
      cout << "-1\n";
      return 0;
    }
    long long int d = it->second;
    bool xshort = false;
    for (it++; it != theS.end();) {
      long long int k1 = it->first;
      long long int d1 = it->second;
      if (R.at(j) <= k1) {
        if (d + C.at(j) < d1) {
          ;
          theS.emplace(R.at(j), d + C.at(j));
        }
        xshort = true;
        break;
      };
      if (d + C.at(j) >= d1) {
        it++;
      } else {
        ;
        it = theS.erase(it);
      }
    }
    if (!xshort) {
      ;
      theS.emplace(R.at(j), d + C.at(j));
    }
  }
  auto it = theS.lower_bound(make_pair(N, 0));
  if (it == theS.end()) {
    cout << "-1\n";
    return 0;
  } else {
    cout << it->second << endl;
    return 0;
  }
}
