#include <bits/stdc++.h>
using namespace std;
using Pi = pair<int, int>;
using ll = long long;
string to_string(string s) { return s; }
template <class S, class T>
string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}
template <class S>
string to_string(unordered_set<S> s) {
  string ret = "{";
  for (S x : s) ret += to_string(x) + ",";
  return ret + "}";
}
template <class S, class T>
string to_string(map<S, T> m) {
  string ret = "{";
  for (pair<S, T> x : m) ret += to_string(x) + ",";
  return ret + "}";
}
template <class T>
string to_string(vector<T> v) {
  string ret = "{";
  for (int i = 0; i < (int)v.size() - 1; ++i) {
    ret += to_string(v[i]) + ",";
  }
  if (v.size() > 0) {
    ret += to_string(v.back());
  }
  ret += "}";
  return ret;
}
void debug() { cerr << endl; }
template <class Head, class... Tail>
void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head head, Tail... tail) {
  cout << to_string(head);
  print(tail...);
}
int main() {
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<ll> dp(N);
  vector<vector<int>> plus(N + 1), minus(N + 1);
  vector<int> costs(M);
  for (int i = 0; i < (int)M; i++) {
    int L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    costs[i] = C;
    plus[L].push_back(i);
    minus[R + 1].push_back(i);
  }
  set<int> now;
  vector<int> taiou(M + 1);
  for (int x : plus[0]) {
    now.insert(costs[x]);
    taiou[x] = costs[x];
  }
  for (int i = 1; i < (int)N; i++) {
    for (int x : minus[i]) {
      now.erase(taiou[x]);
    }
    if (now.empty()) {
      print(-1);
      return 0;
    }
    dp[i] = *now.begin();
    for (int x : plus[i]) {
      now.insert(dp[i] + costs[x]);
      taiou[x] = dp[i] + costs[x];
    }
  }
  print(dp[N - 1]);
  return 0;
}
