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

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

s = XYP.map{|x,y,p| [x.abs,y.abs].min*p }
p s.sum
N.times{
	s = XY.map{|_| _.zip(s).map(&:min) }.min_by(&:sum)
	p s.sum
}
