#require 'pp'
def makeSet(x)
  $ps << x
  $rank << 0
end
def union(x,y)
  link(findSet(x), findSet(y))
end
def link(x,y)
    $ps[y] = x
end
def findSet(x)
  if x != $ps[x]
    $ps[x] = findSet($ps[x])
  end
  return $ps[x]
end
$ps = []
$rank = []

n,q = gets.split.map(&:to_i)
n.times{|i|
  makeSet(i)
}
E = []
q.times{|i|
  E << gets.split.map(&:to_i)
}
E.sort_by!{|x| x[2]}
#pp E

cou = 0
sum = 0
while cou <= n-2
  i = E.shift
#  puts "s:#{findSet(i[0])}"
#  puts "s:#{findSet(i[1])}"
  next if findSet(i[0]) == findSet(i[1])
  union findSet(i[0]), findSet(i[1])
  cou += 1
  sum += i[2]
#  puts sum
  gets
end
puts sum
