#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);
  map<int, long long> ans;
  int idx = 0;
  for (; s[idx].L == 1 && idx < M; ++idx) {
    while (!ans.empty() && prev(ans.end())->second >= s[idx].C)
      ans.erase(prev(ans.end()));
    ans[s[idx].R] = s[idx].C;
  }
  for (; idx < M; ++idx) {
    auto it = ans.lower_bound(s[idx].L);
    if (it == ans.end() || s[idx].R <= it->first) continue;
    pair<int, long long> cur = *it;
    ans.erase(it);
    cur.first = s[idx].R;
    cur.second += s[idx].C;
    if (ans.count(cur.first))
      ans[cur.first] = min(ans[cur.first], cur.second);
    else
      ans[cur.first] = cur.second;
  }
  if (prev(ans.end())->first != N)
    cout << -1;
  else
    cout << prev(ans.end())->second;
}
