#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
inline void nprint() {}
template <class Head, class... Tail>
inline void nprint(Head &&head, Tail &&...tail) {
  cout << head << endl;
  nprint(move(tail)...);
}
using cmp = complex<double>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
template <class T>
using V = vector<T>;
template <class T>
using VV = vector<V<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI = acos(-1), EPS = 1e-10;
constexpr ll MOD = 1E9 + 7;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <class T>
istream &operator>>(istream &is, vector<T> &st) {
  for (size_t i = 0; i < st.size(); ++i) is >> st[i];
  return is;
}
template <class T>
istream &operator>>(istream &is, vector<vector<T>> &st) {
  for (size_t i = 0; i < st.size(); ++i) is >> st[i];
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &st) {
  for (size_t i = 0; i < st.size(); ++i) {
    if (i == st.size() - 1)
      os << st[i];
    else
      os << st[i] << " ";
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &st) {
  for (size_t i = 0; i < st.size(); ++i) {
    os << st[i];
    if (i != st.size() - 1) os << endl;
  }
  return os;
}
struct union_find {
  vector<long long> data;
  vector<long long> diff_weight;
  union_find(long long size, long long abel_sum_unity = 0)
      : data(size, -1), diff_weight(size, abel_sum_unity) {}
  inline long long root(long long a) {
    if (data[a] < 0) return a;
    long long r = root(data[a]);
    diff_weight[a] += diff_weight[data[a]];
    return data[a] = r;
  }
  inline bool same(long long a, long long b) { return root(a) == root(b); }
  inline bool unite(long long a, long long b, long long w = 1) {
    w += weight(a) - weight(b);
    a = root(a), b = root(b);
    if (a == b) return false;
    if (-data[a] < -data[b]) swap(a, b), w = -w;
    data[a] += data[b];
    data[b] = a;
    diff_weight[b] = w;
    return true;
  }
  inline long long size(long long a) { return -data[root(a)]; }
  inline long long weight(long long a) {
    root(a);
    return diff_weight[a];
  }
  inline long long diff(long long a, long long b) {
    return weight(b) - weight(a);
  }
};
struct Edge {
  int src, dst;
  int weight;
  Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
pair<int, vector<Edge>> prim(const vector<vector<Edge>> &g, int r = 0) {
  int n = g.size();
  vector<Edge> T;
  int total = 0;
  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push(Edge(-1, r, 0));
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (visited[e.dst]) continue;
    T.push_back(e);
    total += e.weight;
    visited[e.dst] = true;
    for (auto f = g[e.dst].begin(); f != g[e.dst].end(); ++f)
      if (!visited[f->dst]) Q.push(*f);
  }
  return pair<int, vector<Edge>>(total, T);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, m;
  cin >> n >> m;
  union_find uf(n);
  vector<vector<Edge>> g(n);
  V<tuple<int, int, int>> v(m);
  using tup = tuple<int, int, int>;
  auto f = [](tup l, tup r) { return get<2>(l) > get<2>(r); };
  priority_queue<tup, vector<tup>, decltype(f)> pq(f);
  for (ll i = (0); i < (ll)(m); ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    uf.unite(l, r);
    g[l].emplace_back(l, r, c);
    g[r].emplace_back(r, l, c);
    v[i] = tie(l, r, c);
  }
  sort((v).begin(), (v).end());
  int r = 0;
  int id = 0;
  ll ans = 0;
  while (r != n - 1) {
    while (id < m && get<0>(v[id]) <= r) {
      pq.emplace(v[id++]);
    }
    int rr = r;
    while (rr == r) {
      if (pq.empty()) {
        cout << -1 << endl;
        return 0;
      }
      auto t = pq.top();
      pq.pop();
      if (get<1>(t) <= rr) continue;
      rr = get<1>(t);
      ans += get<2>(t);
    }
    r = rr;
  }
  cout << ans << endl;
}
