class DisjointSet

  def initialize n
    @ps = n.times.map.to_a
  end

  def union x, y
    x = find x
    y = find y
    @ps[y] = x
  end

  def find x
    if x == @ps[x]
      x
    else
      @ps[x] = find @ps[x]
    end
  end

end

n, m = gets.split.map &:to_i
ds = DisjointSet.new n
es = m.times.map { gets.split.map(&:to_i) }.sort_by!(&:last).reverse!
c, r = n, 0
until c == 1
  s, t, w = es.pop
  s, t = ds.find(s), ds.find(t)
  next if s == t
  ds.union s, t
  c -= 1
  r += w
end
p r