if __FILE__ == $0

	class UnionFindTree
		attr_reader :n, :parent , :rank

		def initialize(n)
			@n =n
			@parent = Array.new(n){ |idx| idx }
			@rank = Array.new(n,0)
		end

		def root(x)
			return parent[x]==x ? x : parent[x]=root(parent[x])
		end
		
		def is_same(x,y)
			return root(x)==root(y)
		end

		def unite(x,y)
			x=root(x)
			y=root(y)

			return false if x==y

			if rank[x]<rank[y]
				parent[x]=y
			else
				parent[y]=x
				rank[y]+=1 if rank[x]==rank[y]
			end
			
			return true
		end
		
	end

	class Edge

		attr_reader :from,:to,:cost
		include Comparable

		def initialize(from,to,cost)
			@from =from
			@to = to
			@cost = cost
		end

		def <=>(other)
			return @cost - other.cost
		end

	end

	n,m = gets.split.map(&:to_i)
	uft = UnionFindTree.new(n)
	
	edges = []
	m.times do
		s,t,w = gets.split.map(&:to_i)
		edges << Edge.new(s,t,w)
	end
	
	edges.sort!

	
	res=0
	edges.each do |e|
		if !uft.is_same(e.from,e.to)
			uft.unite(e.from,e.to)
			res+=e.cost
		end
	end

	puts res


end