import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



import operator



N,H,W = list(map(int,readline().split()))

m = list(map(int,read().split()))

RCA = sorted(zip(m,m,m),key=operator.itemgetter(2),reverse=True)



root = list(range(H+W))

size = [0] * (H+W)

no_cycle = [True] * (H+W)



def find_root(x):

    path = []

    while True:

        y = root[x]

        if x == y:

            break

        path.append(x)

        x = y

    for p in path:

        root[p] = x

    return root[x]



def merge(x,y):

    x,y = find_root(x),find_root(y)

    sx,sy = size[x],size[y]

    if sx < sy:

        sx,sy = sy,sx

        x,y = y,x

    root[y] = x

    no_cycle[x] = no_cycle[x] and no_cycle[y]



answer = 0

for R,C,A in RCA:

    x,y = R-1,H+C-1

    rx,ry = find_root(x),find_root(y)

    if rx == ry:

        if not no_cycle[rx]:

            continue

        no_cycle[rx] = False

    else:

        if (not no_cycle[rx]) and (not no_cycle[ry]):

            continue

        merge(rx,ry)

    answer += A



print(answer)
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10**7)



import operator



N,H,W = list(map(int,readline().split()))

m = list(map(int,read().split()))

RCA = sorted(zip(m,m,m),key=operator.itemgetter(2),reverse=True)



root = list(range(H+W))

size = [0] * (H+W)

no_cycle = [True] * (H+W)



def find_root(x):

    if x == root[x]:

        return x

    path = []

    while True:

        y = root[x]

        if x == y:

            break

        path.append(x)

        x = y

    for p in path:

        root[p] = x

    return root[x]



def merge(x,y):

    x,y = find_root(x),find_root(y)

    sx,sy = size[x],size[y]

    if sx < sy:

        sx,sy = sy,sx

        x,y = y,x

    root[y] = x

    no_cycle[x] = no_cycle[x] and no_cycle[y]



answer = 0

for R,C,A in RCA:

    x,y = R-1,H+C-1

    rx,ry = find_root(x),find_root(y)

    if rx == ry:

        if not no_cycle[rx]:

            continue

        no_cycle[rx] = False

    else:

        if (not no_cycle[rx]) and (not no_cycle[ry]):

            continue

        merge(rx,ry)

    answer += A



print(answer)
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import operator

N, H, W = list(map(int, readline().split()))
m = list(map(int, read().split()))
RCA = sorted(zip(m, m, m), key=operator.itemgetter(2), reverse=True)
root = list(range(H + W))
size = [0] * (H + W)
no_cycle = [True] * (H + W)


def find_root(x):
    path = []
    while True:
        y = root[x]
        if x == y:
            break
        path.append(x)
        x = y
    for p in path:
        root[p] = x
    return root[x]


def merge(x, y):
    x, y = find_root(x), find_root(y)
    sx, sy = size[x], size[y]
    if sx < sy:
        sx, sy = sy, sx
        x, y = y, x
    root[y] = x
    no_cycle[x] = no_cycle[x] and no_cycle[y]


answer = 0
for R, C, A in RCA:
    x, y = R - 1, H + C - 1
    rx, ry = find_root(x), find_root(y)
    if rx == ry:
        if not no_cycle[rx]:
            continue
        no_cycle[rx] = False
    else:
        if (not no_cycle[rx]) and (not no_cycle[ry]):
            continue
        merge(rx, ry)
    answer += A
print(answer)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
import operator

N, H, W = list(map(int, readline().split()))
m = list(map(int, read().split()))
RCA = sorted(zip(m, m, m), key=operator.itemgetter(2), reverse=True)
root = list(range(H + W))
size = [0] * (H + W)
no_cycle = [True] * (H + W)


def find_root(x):
    if x == root[x]:
        return x
    path = []
    while True:
        y = root[x]
        if x == y:
            break
        path.append(x)
        x = y
    for p in path:
        root[p] = x
    return root[x]


def merge(x, y):
    x, y = find_root(x), find_root(y)
    sx, sy = size[x], size[y]
    if sx < sy:
        sx, sy = sy, sx
        x, y = y, x
    root[y] = x
    no_cycle[x] = no_cycle[x] and no_cycle[y]


answer = 0
for R, C, A in RCA:
    x, y = R - 1, H + C - 1
    rx, ry = find_root(x), find_root(y)
    if rx == ry:
        if not no_cycle[rx]:
            continue
        no_cycle[rx] = False
    else:
        if (not no_cycle[rx]) and (not no_cycle[ry]):
            continue
        merge(rx, ry)
    answer += A
print(answer)

+sys.setrecursionlimit(10**7) +    if x == root[x]: +        return x