#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
const int MOD = (int)1e9 + 7;
const long long INF = 1LL << 62;
const int inf = 1 << 30;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& V) {
  int N = V.size();
  for (int i = 0; i < (N); i++) {
    os << V[i];
    if (i != N - 1) os << " ";
  }
  os << "\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, pair<T, T> const& P) {
  os << P.first;
  os << " ";
  os << P.second;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& S) {
  auto it = S.begin();
  while (it != S.end()) {
    os << *it;
    os << " ";
    it++;
  }
  os << "\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T>& q) {
  for (auto it = q.begin(); it < q.end(); it++) {
    os << *it;
    os << " ";
  }
  os << endl;
  return os;
}
vector<pair<int, int>> dxdy = {make_pair(0, 1), make_pair(1, 0),
                               make_pair(-1, 0), make_pair(0, -1)};
template <typename T>
struct SegmentTree {
  using F = function<T(T, T)>;
  int N;
  F func;
  T identity;
  vector<T> data;
  SegmentTree() {}
  SegmentTree(F f, T identity) : func(f), identity(identity) {}
  void init(int n_) {
    N = 1;
    while (N < n_) N <<= 1;
    data.assign(N << 1, identity);
  }
  void build(const vector<T>& v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++) {
      data[N + i] = v[i];
    }
    for (int i = n_ - 1; i; i++) {
      data[i] = func(data[i << 1], data[(i << 1) + 1]);
    }
  }
  void set_val(int k, T x) {
    k += N;
    data[k] = x;
    while (k >>= 1) {
      data[k] = func(data[k << 1], data[(k << 1) + 1]);
    }
  }
  T query(int left, int right) {
    T left_val = identity, right_val = identity;
    for (int l = left + N, r = right + N; l < r; l >>= 1, r >>= 1) {
      if (l & 1) left_val = func(left_val, data[l++]);
      if (r & 1) right_val = func(data[--r], right_val);
    }
    return func(left_val, right_val);
  }
  T get_val(int idx) { return data[idx + N]; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  SegmentTree<int> S([](int a, int b) -> int { return min(a, b); }, 2147483647);
  S.init(N);
  S.set_val(0, 0);
  vector<tuple<int, int, int>> query;
  int l, r, c;
  for (int i = 0; i < (M); i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    query.emplace_back(l, r, c);
  }
  sort((query).begin(), (query).end());
  for (int i = 0; i < (M); i++) {
    tie(l, r, c) = query[i];
    auto from = S.query(l, r);
    if (from == S.identity) {
      cout << "-1\n";
      return 0;
    }
    auto to = S.query(r, r + 1);
    if (to > from + c) S.set_val(r, from + c);
  }
  int ans = S.get_val(N - 1);
  cout << ans << endl;
}
