

  module UnionFind
    implicit none
    public
        type :: Union_Find
            integer(8),allocatable,dimension(:) :: par
        end type Union_Find
    contains
        subroutine init(p,n)
            implicit none
            type(Union_Find)  p
            integer(8) n,i
            allocate( p%par(n) )
            do i=1,n
                p%par(i) = i
            enddo
        end subroutine init
        recursive function root(p,n) result(ret)
            implicit none
            type(Union_Find) p
            integer(8) ret,n
            if(p%par(n)==n)then
                ret = n
                return
            else
                p%par(n) = root(p,p%par(n))
                ret = p%par(n)
                return
            endif
        end
        subroutine unite(p,a,b)
            implicit none
            type(Union_Find) p
            integer(8) a,b
            a = root(p,a)
            b = root(p,b)
            if(a/=b)then
                p%par(a) = b
            endif
        end
end module UnionFind








program atcoder
    use UnionFind
    implicit none
    type(Union_Find) uf
    integer(8) :: n,k,i,ans=0_8,j,h,w
    integer(8),allocatable,dimension(:) :: a1,a2,ind,r,c
    read*,n,h,w
    call init(uf,h+w+5)
    allocate( a1(n),a2(n),ind(n),r(n),c(n) )
    do i=1,n
        read(*,*)r(i),c(i),a1(i)
        ind(i) = i;
        a2(i) = a1(i);
    enddo
    call sort(a1,ind)
    a1 = a2
    call sort(a1,r)
    a1 = a2
    call sort(a1,c)

    do i = n,1,-1
        if(root(uf,r(i))/=root(uf,c(i)+h))then
            call unite(uf,r(i),c(i)+h)
            ans = ans + a1(i)
        else if(root(uf,r(i))/=root(uf,h+w+3))then
            ans = ans + a1(i)
            call unite(uf,r(i),h+w+3)
        endif
    enddo
    write(*,'(I0)')ans


  !----------------------functions--------------------------
  contains
  recursive subroutine mergesort(a, p, r, b,ia,ib)
  integer(8) a(:), b(:),ia(:),ib(:)
  integer(8) p, r
  integer(8) q
  if (p < r) then
    q = (p+r)/2
    call mergesort(a, p, q, b,ia,ib)
    call mergesort(a, q+1, r, b,ia,ib)
    call merge(a, p, q, r, b, ia,ib)
  end if
  end subroutine mergesort
  subroutine merge(a, p, q, r, b, ia,ib)
    integer(8) a(:), b(:),ia(:),ib(:)
    integer(8) p, r, q, i, j, k
    integer(8) t,ti
    k = p;   i = p;   j = q+1
    do
      if (.not. (k <= r)) exit
      if (j > r) then
        t = a(i); ti = ia(i); i = i+1
      else if (i > q) then
        t = a(j); ti = ia(j); j = j+1
        
      else if (a(i) <= a(j)) then
        t = a(i); ti = ia(i); i = i+1
      else
        t = a(j); ti = ia(j); j = j+1
      end if
      b(k) = t; ib(k) = ti; k = k+1

    end do
    do i = p, r
      a(i) = b(i)
      ia(i) = ib(i)
    end do
  end subroutine merge
  subroutine sort(a,ia)
    integer(8) a(:), b(size(a)),n,ia(:),ib(size(a))
    n = size(a)
    call mergesort(a,1_8,n,b,ia,ib)
  end subroutine sort
  end program atcoder






