#include <bits/stdc++.h>
using namespace std;
struct Seg {
  int L, R, C;
  bool operator<(const Seg s) const {
    if (L == s.L) {
      if (R == s.R) return C < s.C;
      return R < s.R;
    }
    return L < s.L;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  Seg s[M];
  for (int i = 0; i <= M - 1; ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    s[i] = {L, R, C};
  }
  sort(s, s + M);
  deque<pair<int, long long> > ans;
  int idx = 0;
  for (; idx < M && s[idx].L == 1; ++idx) {
    auto cur = make_pair(s[idx].R, s[idx].C);
    while (!ans.empty() && ans.back().first >= cur.first &&
           ans.back().second >= cur.second)
      ans.pop_back();
    if (ans.empty() || ans.back().first < cur.first) ans.push_back(cur);
  }
  for (; idx < M; ++idx) {
    auto it = lower_bound(begin(ans), end(ans), make_pair(s[idx].L, 0LL));
    if (it == ans.end() || s[idx].R <= it->first) continue;
    auto cur = make_pair(s[idx].R, it->second + s[idx].C);
    while (!ans.empty() && ans.back().first >= cur.first &&
           ans.back().second >= cur.second)
      ans.pop_back();
    if (ans.empty() || ans.back().first < cur.first) ans.push_back(cur);
  }
  if (ans.empty() || ans.back().first != N)
    cout << -1;
  else
    cout << ans.back().second;
}
