module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end

include Memoizable

require 'set'

def f(graph, color, taken)
	heads = (0..graph.size-1).select {|i| !taken.include?(i) && graph[i].all? {|j| taken.include?(j)}}
	max = 0
	heads.combination(2) do |i, j|
		next if color[i] != color[j]
		r = 2 + f(graph, color, taken + [i, j])
		max = r if r > max
	end
	max
end

memoize :f

loop do
	n = gets.to_i
	break if n == 0
	disks = (1..n).map { gets.split.map(&:to_i)	}
	color = disks.map(&:last)
	graph = {}
	(disks.size-1).downto(0) do |i|
		graph[i] = Set.new
		x1, y1, r1, c1 = disks[i]
		(0..i-1).each do |j|
			x2, y2, r2, c2 = disks[j]
			if (x1 - x2) ** 2 + (y1 - y2) ** 2 < (r1 + r2) ** 2
				graph[i] << j
			end
		end
	end
	p f(graph, color, Set.new)
end