class UnionFind
  def initialize(n)
    @par = Array.new(n){|x| x}
    @size = Array.new(n, 0)
  end

  def find(x)
    return x if x == @par[x]
    return @par[x] = find(@par[x])
  end

  def unite(x, y)
    x = find(x)
    y = find(y)
    return if x == y
    swap(x, y) if @size[x] < @size[y]
    @par[y] = x
    @size[x] += @size[y]
  end

  def same(x, y)
    return find(x) == find(y)
  end

  def size(x)
    return @size[find(x)]
  end
end

class Edge
  def initialize
    @a, @b, @cost = 0, 0, 0
  end
end

class Graph
  def initialize(n, es)
    @n = n
    @es = es
  end

  def kruskal()
    @es = @es.sort_by{|x| x[2]}

    uf = UnionFind.new(@n)
    min_cost = 0
    @es.each{|e|
      if !uf.same(e[0], e[1])
        min_cost += e[2]
        uf.unite(e[0], e[1])
      end
    }
    return min_cost
  end
end

v, e = gets.split.map(&:to_i)
es = e.times.map { gets.split.map(&:to_i) }

g = Graph.new(v, es)
p g.kruskal()