# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<LL>>g;
vector<edge>edges;
vector<Pll>v;
map<Pll, Pll>ma;
set<LL>st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, a[1000][1000], b[210000];
string str, ss;
bool f;
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
bool namori[300000] = {};
struct QuickFind {
	vector<int> gi; //親
	vector<vector<int>> vg; //併合した後削除する
	vector<vector<int>> tg; //併合した後削除しない
	int n;

	QuickFind(int size) : n(size) {
		gi.resize(n);
		vg.resize(n);
		tg.resize(n);
		for (int i = 0; i < n; i++) {
			gi[i] = i;
			vg[i].push_back(i);
			tg[i].push_back(i);
		}
	}
	bool same_group(int a, int b) {
		return gi[a] == gi[b];
	}
	void merge(int a, int b) {
		int ga = gi[a];
		int gb = gi[b];
		if (ga == gb) return;
		if (vg[ga].size() < vg[gb].size()) swap(ga, gb);
		for (int j : vg[gb]) gi[j] = ga;
		vg[ga].insert(vg[ga].end(), vg[gb].begin(), vg[gb].end());
		vg[gb].clear();
		tg[ga].insert(tg[ga].end(), tg[gb].begin(), tg[gb].end());
	}
};
int main() {
	cin >> n >> h >> w;
	QuickFind qf(h+w+1);
	rep(i, n) {
		LL x, y, z;
		cin >> x >> y >> z;
		x--, y--;
		edges.push_back(edge{ x,y+h,z });
	}
	sort(edges.begin(), edges.end(), [](edge x, edge y) {
		return (x.cost > y.cost);
	});
	
	for(int i=0;i<n;i++) {
		LL s = edges[i].from, t = edges[i].to;
		if (!qf.same_group(s, t)) {
			if (namori[qf.gi[s]] && namori[qf.gi[t]])continue;
			bool F = (namori[qf.gi[s]] || namori[qf.gi[t]]);
			qf.merge(s, t);
			namori[qf.gi[s]] |= F;
			ans += edges[i].cost;
		}
		else if (namori[qf.gi[s]] == 0) {
			namori[qf.gi[s]] = true;
			ans += edges[i].cost;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 32880