class DisjointSet
  def initialize(size = nil)
    @rank = Array.new(size){0}
    @parent = []
    if size
      size.times do |i|
        @parent[i] = i
      end
    end
  end

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

  def unite(x, y)
    x = find(x)
    y = find(y)
    if @rank[x] > @rank[y]
      @parent[y] = x
    else
      @parent[x] = y
      @rank[y] += 1 if @rank[x] == @rank[y]
    end
  end

  def find(x)
    @parent[x] = find(@parent[x]) unless x == @parent[x]
    return @parent[x]
  end
end

v, e = gets.split.map(&:to_i)
tree = DisjointSet.new(v)

edges = {}
e.times{|i|
  s, t, w = gets.split.map(&:to_i)
  edges["#{s} #{t}"] = w
}

edges = edges.sort_by {|k, val| val}
sum = 0
edges.each{|edge|
  s, t = edge[0].split.map(&:to_i)
  unless tree.same(s, t) then
    sum += edge[1]
    tree.unite(s, t)
  end
}
puts sum