class UnionFind
  def initialize(n)
    @size = Array.new(n + 1, 1)
    @parent = []

    n.times do |i|
      @parent[i] = i
    end
  end

  def find(x)
    if @parent[x] == x
      x
    else
      @parent[x] = find(@parent[x])
    end
  end

  def unite(x, y)
    x = find(x)
    y = find(y)
    return if x == y

    @parent[y] = x
    @size[x] += @size[y]
  end

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

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

def kruskal(v, edges)
  edges.sort_by! { |from, to, cost| cost }
  uf = UnionFind.new(v)

  total_cost = 0

  edges.each do |from, to, cost|
    unless uf.same?(from, to)
      uf.unite(from, to)
      total_cost += cost
    end
  end

  total_cost
end

V, E = gets.split.map(&:to_i)
edges = E.times.map { gets.split.map(&:to_i) }

puts kruskal(V, edges)

