#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

const int maxn = 2e5 + 10;
int n, m;
vector<int> graph[maxn];
int parent[maxn], sz[maxn];
 
void init() {
    for(int i = 0; i < maxn; i++) {
        parent[i] = i;
        sz[i] = 1;
    }
}
int find_root(int x) {
    while(x != parent[x]) {
        parent[x] = parent[parent[x]];
        x = parent[x];
    }
    return x;
}
bool check(int A, int B) {
    return find_root(A) == find_root(B);
}
void unite(int A, int B) {
    int root_a = find_root(A);
    int root_b = find_root(B);
     
    if(root_a != root_b) {
        if(sz[root_a] < sz[root_b]) {
            sz[root_b] += sz[root_a];
            parent[root_a] = parent[root_b];
        }
        else {
            sz[root_a] += sz[root_b];
            parent[root_b] = parent[root_a];
        }
    }
}

vector<int> or_graph[maxn];
bool check_N(int S) {
    queue<int> q;
    q.push(S);
    int vis = 0;
    vector<bool> visited(n, false);
    visited[S] = true;
    while(!q.empty()) {
        int c = q.front();
        q.pop();
        vis++;
        
        for(int i = 0; i < (int) or_graph[c].size(); i++) {
            int neighbour = or_graph[c][i];
            if(!visited[neighbour]) {
                visited[neighbour] = true;
                q.push(neighbour);
            }
        }
    }
    if(vis == n) {
        return true;
    }
    return false;
}
int main() {
    ios_base::sync_with_stdio(false);
//    ifstream cin("in.txt");
    cin >> n >> m;
    init();
    
    vector<pair<int, pair<int, int>>> v;
    for(int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        or_graph[a].push_back(b);
        or_graph[b].push_back(a);
        
        v.push_back(make_pair(c, make_pair(a, b)));
    }
    if(!check_N(0)) {
        cout << "N" << endl;
        return 0;
    }
    sort(v.rbegin(), v.rend());
    long long res = 0;
    for(int i = 0; i < m; i++) {
        int weight = v[i].first;
        int a = v[i].second.first;
        int b = v[i].second.second;
        
        
        if(!check(a, b) or weight > 0) {
            unite(a, b);
            res += weight;
        }
        
        
    }
    cout << res << endl;
}
