#include <bits/stdc++.h>
struct Operation {
  int left, right;
  long long int cost;
};
class MinSegmentTree {
  std::vector<std::vector<long long int>> vec;

 public:
  MinSegmentTree(const int size)
      : vec{std::vector<long long int>(size, LLONG_MAX)} {
    while (vec.back().size() > 1) {
      vec.emplace_back(vec.back().size() / 2, LLONG_MAX);
    }
    for (auto i = 1; i < vec.size(); ++i) {
      for (auto j = 0; j < vec[i].size(); ++j) {
        vec[i][j] = std::min(vec[i - 1][j * 2], vec[i - 1][j * 2 + 1]);
      }
    }
  }
  long long int get(int from, int to) const {
    long long int result = LLONG_MAX;
    for (auto i = 0; i < vec.size(); ++i) {
      if (from % 2 == 1) {
        result = std::min(result, vec[i][from]);
        ++from;
      }
      if (to % 2 == 0) {
        result = std::min(result, vec[i][to]);
        --to;
      }
      if (from > to) {
        return result;
      }
      from /= 2;
      to /= 2;
    }
    return result;
  }
  void set(int position, long long int new_value) {
    vec[0][position] = new_value;
    position /= 2;
    for (auto i = 1; i < vec.size(); ++i) {
      if (position >= vec[i].size()) return;
      vec[i][position] =
          std::min(vec[i - 1][position * 2], vec[i - 1][position * 2 + 1]);
    }
  }
};
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<Operation> operations(m);
  for (auto& op : operations) {
    std::cin >> op.left >> op.right >> op.cost;
    --op.left;
    --op.right;
  }
  std::sort(operations.begin(), operations.end(),
            [](const Operation& a, const Operation& b) {
              return a.right == b.right ? a.left < b.left : a.right < b.right;
            });
  std::vector<long long int> min_value(n, LLONG_MAX);
  min_value[0] = 0;
  MinSegmentTree seg(n);
  seg.set(0, 0);
  for (const auto& op : operations) {
    const auto min = seg.get(op.left, op.right);
    if (min != LLONG_MAX && min_value[op.right] > op.cost + min) {
      min_value[op.right] = op.cost + min;
      seg.set(op.right, op.cost + min);
    }
  }
  const auto result = seg.get(n - 1, n - 1);
  if (result == LLONG_MAX) {
    std::cout << -1 << '\n';
  } else {
    std::cout << result << '\n';
  }
}
