#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec) is >> x;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}\n   ";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
void dump_func() { cerr << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  cerr << head;
  if (sizeof...(Tail) > 0) {
    cerr << ", ";
  }
  dump_func(std::move(tail)...);
}
bool comp_by_first(tuple<int, int, int> a, tuple<int, int, int> b) {
  return get<0>(a) < get<0>(b);
}
int main() {
  int N, M;
  cin >> N >> M;
  auto path = vector<tuple<int, int, int>>(M);
  for (int i = 0, i_len = (M); i < i_len; ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    path[i] = {L, R, C};
  }
  int INF = 1000000001;
  auto result = vector<int>(N, INF);
  sort((path).begin(), (path).end(), comp_by_first);
  result[0] = 0;
  for (int i = 0, i_len = (M); i < i_len; ++i) {
    auto t = path[i];
    int L = get<0>(t);
    int R = get<1>(t);
    int C = get<2>(t);
    for (int j = L, j_len = (R); j < j_len; ++j) {
      result[R] = min(result[R], result[j] + C);
    }
  };
  if (result[N - 1] != INF) {
    cout << result[N - 1] << endl;
  } else {
    cout << -1 << endl;
  }
}
