#include <bits/stdc++.h>
const int inf = 1e9 + 7;
const long long infll = 1ll << 60ll;
const long long mod = 1e9 + 7;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
template <typename T>
void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <typename T>
void chmin(T& a, T b) {
  a = std::min(a, b);
}
template <typename T>
void chadd(T& a, T b) {
  a = a + b;
}
namespace {
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
}  // namespace
struct Edge {
  long long l, r, c;
};
int main() {
  long long n, m;
  std::cin >> n >> m;
  std::vector<Edge> edges(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> edges[i].l >> edges[i].r >> edges[i].c;
  }
  std::sort((edges).begin(), (edges).end(), [](Edge lhs, Edge rhs) {
    if (lhs.l == rhs.l) return lhs.c < rhs.c;
    return lhs.l < rhs.l;
  });
  std::vector<long long> dp(n + 1, infll);
  dp[0] = 0LL;
  for (int i = 1; i < m; ++i) {
    if (edges[i].l == edges[i - 1].l) {
      if (edges[i].r < edges[i - 1].r) {
        edges[i].r = edges[i - 1].r;
      }
    }
  }
  long long left = 0;
  for (long long i = 1; i <= n; ++i) {
    if (left == m) break;
    if (dp[i - 1] == infll) continue;
    while (i >= edges[left].l) {
      dp[edges[left].r - 1] =
          std::min(dp[edges[left].r - 1], dp[i - 1] + edges[left].c);
      ++left;
      if (left == m) break;
    }
  }
  if (dp[n - 1] == infll) {
    std::cout << -1 << std::endl;
  } else
    std::cout << dp[n - 1] << std::endl;
  return 0;
}
