#include <bits/stdc++.h>
using namespace std;
struct tree {
  long long int val;
  int l, r;
  struct tree* left;
  struct tree* right;
  void insert(long long int, int, int);
  long long int rangeMin(int, int);
};
using Tree = struct tree*;
Tree makeNode(long long int nv, int nl, int nr) {
  if (nr < nl) return nullptr;
  Tree node = new struct tree;
  node->val = nv;
  node->l = nl, node->r = nr;
  node->left = nullptr, node->right = nullptr;
  return node;
}
void tree::insert(long long int nv, int nl, int nr) {
  if (nr < nl) return;
  if (l == r) {
    val = min(val, nv);
    if (left == nullptr)
      left = makeNode(nv, nl, l - 1);
    else
      left->insert(nv, nl, l - 1);
    if (right == nullptr)
      right = makeNode(nv, r + 1, nr);
    else
      right->insert(nv, r + 1, nr);
  } else {
    int tl = l, tr = r;
    l = nl, r = nl;
    this->left = makeNode(val, tl, nl - 1);
    this->right = makeNode(min(val, nv), nr, nr);
    this->right->left = makeNode(min(val, nv), nl + 1, nr - 1);
    this->right->right = makeNode(val, nr + 1, tr);
    val = min(val, nv);
  }
}
long long int tree::rangeMin(int nl, int nr) {
  if (nl > nr) return 0x3f3f3f3f3f3f3f3f;
  if (nl >= l && r >= nr) return val;
  long long int res = 0x3f3f3f3f3f3f3f3f;
  if (nl > r || nr < l)
    res = 0x3f3f3f3f3f3f3f3f;
  else
    res = val;
  if (left != nullptr && l == r)
    res = min(res, left->rangeMin(nl, min(nr, l - 1)));
  if (right != nullptr && l == r)
    res = min(res, right->rangeMin(max(nl, r + 1), nr));
  return res;
}
void printTree(Tree root) {
  if (root == nullptr) return;
  printTree(root->left);
  cout << root->l << root->r << root->val << " ";
  printTree(root->right);
}
int main(int argc, char* argv[]) {
  vector<pair<int, pair<int, long long int>>> vp;
  int n, m;
  cin >> n >> m;
  Tree root = makeNode(0x3f3f3f3f3f3f3f3f, 1, n);
  root->insert(0, 1, 1);
  for (int i = 0; i < m; i++) {
    long long int l, r, c;
    cin >> l >> r >> c;
    vp.push_back(make_pair(l, make_pair(r, c)));
  }
  sort(vp.begin(), vp.end());
  for (auto p : vp) {
    long long int t = root->rangeMin(p.first, p.second.first);
    root->insert(p.second.second + t, p.first, p.second.first);
  }
  while (root->right != nullptr) {
    root = root->right;
  }
  if (root->val == 0x3f3f3f3f3f3f3f3f) root->val = -1;
  cout << root->val << endl;
}
