#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 4;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;
int N, M;
vector<pair<int, int> > V[MAXN];
vector<long long> A[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < (int)(M); ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    V[l].emplace_back(c, r);
  }
  multiset<long long> S;
  S.insert(0);
  A[1].push_back(0);
  for (int i = 0; i < (int)(N); ++i) {
    for (long long a : A[i]) S.erase(a);
    if (!S.size()) do {
        cout << -1 << '\n';
        return 0;
      } while (0);
    long long mn = *S.begin();
    if (i == N - 1) {
      cout << mn << endl;
      return 0;
    }
    for (auto v : V[i]) {
      long long tmp = mn + v.first;
      S.insert(tmp);
      A[v.second + 1].push_back(tmp);
    }
  }
  return 0;
}
