(N,),*XYP = $<.map{|ln| ln.split.map(&:to_i) }

P = XYP.map{|x,y,p| [x.abs,y.abs].min*p }
X = XYP.product(XYP).map{|(t,_,_),(x,y,p)|
	(x-t).abs*p
}.each_slice(N).to_a
Y = XYP.product(XYP).map{|(_,t,_),(x,y,p)|
	(y-t).abs*p
}.each_slice(N).to_a
XY = X+Y

S = [Float::INFINITY]*(N+1)
F = lambda{|a,i,k|
	next if N<k

	s = a.sum
	S[k] = s if s < S[k]

	(i...XY.size).each{|i|
		b = a.map.with_index{|_,j| [_,XY[i][j]].min }
		F[b,i+1,k+1]
	}
}
F[P,0,0]

puts S
