import collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
import scala.language.implicitConversions


trait Graph {
    case class Edge[T:Numeric](from : Int, to : Int, cost : T)

    class Edges[T:Numeric](val data : ArrayBuffer[Edge[T]]) {
        def size = data.size
    }
    object Edges {
        def apply[T:Numeric](xs : Edge[T]*) = new Edges(ArrayBuffer(xs:_*))
        implicit def toUnderLying[T](x:Edges[T]) = x.data
    }

    class WeightedGraph[T:Numeric](val size : Int, val data : Array[Edges[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(Edges()))
    }
    object WeightedGraph {
        def apply[T:Numeric](size : Int) = new WeightedGraph[T](size)
        implicit def toUnderLying[T](x:WeightedGraph[T]) = x.data
    }

    class UnWeightedGraph[T:Numeric](val size : Int, val data: Array[ArrayBuffer[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(ArrayBuffer[T]()))
    }
    object UnWeightedGraph {
        def apply[T:Numeric](size : Int) = new UnWeightedGraph[T](size)
        implicit def toUnderLying[T](x:UnWeightedGraph[T]) = x.data
    }
   
    class Matrix[T:Numeric:ClassTag](val row : Int, val col : Int, val data : Array[Array[T]]) {
        def this(row : Int, col : Int) = this(row, col, Array.fill(row, col)(implicitly[Numeric[T]].zero))
        def this(size : Int) = this(size, size)
    }
    object Matrix {
        def apply[T:Numeric:ClassTag](row : Int, col : Int)(init : T) = 
            new Matrix[T](row, col, Array.fill(row, col)(init))
        def apply[T:Numeric:ClassTag](size : Int)(init : T) : Matrix[T] = Matrix[T](size, size)(init)
        implicit def toUnderLying[T](x:Matrix[T]) = x.data
    }
}

object Prim extends Graph {
    import scala.collection.mutable.PriorityQueue
    def prim[T](g : WeightedGraph[T])(implicit num : Numeric[T]) : T = {
        import num._
        val used = new Array[Boolean](g.size)
        val que = PriorityQueue()(Ordering.Tuple2[T, Int].reverse)
        que += ((zero, 0))
        var res = zero
        while (!que.isEmpty) {
            val (d, v) = que.dequeue
            if (!used(v)) {
                res = res + d
                used(v) = true
                for (e <- g(v)) {
                    que += ((e.cost, e.to))
                }
            }
        }
        res
    }
}

import Prim._

object Main {
    def main(args: Array[String]) = {
        val sc = new java.util.Scanner(System.in)
        val V, E = sc.nextInt
        val edges = WeightedGraph[Int](V)
        for (_ <- 1 to E) {
            val s, t, c = sc.nextInt
            edges(s) += Edge(s, t, c)
            edges(t) += Edge(t, s, c)
        }
        val res = prim(edges)
        println(res)
    }
}

