#include <bits/stdc++.h>
using ll = long long;
using P = std::pair<ll, ll>;
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}
ll pow_mod(ll a, ll b, ll mod = -1) {
  if ((a == 0) || (mod != -1 && a % mod == 0)) {
    return 0;
  }
  ll x = 1;
  while (b > 0) {
    if (b & 1) {
      x = (mod != -1) ? (x * a) % mod : x * a;
    }
    a = (mod != -1) ? (a * a) % mod : a * a;
    b >>= 1;
  }
  return x;
}
const ll MOD = 1e9 + 7;
class Edge {
 public:
  ll l, r, cost;
};
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll n, m;
  std::cin >> n >> m;
  std::vector<Edge> edge(m);
  for (ll(i) = (0); i < (m); i++) {
    ll l, r, c;
    std::cin >> l >> r >> c;
    edge[i] = {l, r, c};
  }
  std::sort(edge.begin(), edge.end(), [](Edge t1, Edge t2) {
    if (t1.l != t2.l)
      return t1.l < t2.l;
    else if (t1.cost != t2.cost)
      return t1.cost < t2.cost;
    else
      return t1.r > t2.l;
  });
  ll ans = 0;
  ll now = 1;
  ll itr = 0;
  std::priority_queue<Edge, std::vector<Edge>, std::function<bool(Edge, Edge)>>
      que([](Edge a, Edge b) {
        if (a.cost != b.cost)
          return a.cost > b.cost;
        else
          a.r < b.r;
      });
  while (now != n) {
    while (itr != n && edge[itr].l <= now) {
      que.push(edge[itr]);
      itr++;
    }
    bool flag = true;
    while (flag) {
      flag = false;
      if (que.empty()) {
        std::cout << -1;
        return 0;
      }
      Edge e = que.top();
      que.pop();
      if (e.r <= now)
        flag = true;
      else {
        now = e.r;
        ans += e.cost;
      }
    }
  }
  std::cout << ans;
  return 0;
}
