#include <bits/stdc++.h>
using namespace std;
template <class... A>
void prints() {
  cout << "\n";
}
template <class... A>
void prints_rest() {
  cout << "\n";
}
template <class T, class... A>
void prints_rest(const T &first, const A &...rest) {
  cout << " " << first;
  prints_rest(rest...);
}
template <class T, class... A>
void prints(const T &first, const A &...rest) {
  cout << first;
  prints_rest(rest...);
}
const long long llinf = numeric_limits<long long>::max();
const long long _llinf = numeric_limits<long long>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const long long MOD = 1000000007;
const long long BIG = 10000000000000000;
vector<tuple<int, int, int>> elist[100005];
long long dist[100005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) dist[i] = BIG;
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    elist[r].push_back(make_tuple(c, l, r));
  }
  int lnow = n;
  int rdone = n + 1;
  priority_queue<tuple<int, int, int>, std::vector<tuple<int, int, int>>,
                 std::greater<tuple<int, int, int>>>
      que;
  for (int i = lnow; i < rdone; i++) {
    for (const auto &e : elist[i]) {
      que.push(e);
    }
  }
  while (!que.empty()) {
    tuple<int, int, int> tmp = que.top();
    que.pop();
    int ctmp, ltmp, rtmp;
    ctmp = get<0>(tmp);
    ltmp = get<1>(tmp);
    rtmp = get<2>(tmp);
    if (ltmp < lnow) {
      for (int i = ltmp; i < lnow; i++) {
        dist[i] = dist[rtmp] + ctmp;
      }
      lnow = ltmp;
      for (int i = lnow; i < rdone; i++) {
        for (const auto &e : elist[i]) {
          que.push(e);
        }
      }
      if (lnow == 1) {
        cout << dist[1] << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
