require 'set'

def f(color, g)
    tops = g.keys.select {|i| g[i].empty?}
    cache = $memo[tops]
    return cache if cache

    min = g.keys.size
    groups = tops.group_by {|i| color[i]}
    groups.values.each do |group|
        group.combination(2) do |i, j|
            copy = g.map {|k, a| [k, a]}.to_h
            copy.delete(i)
            copy.keys.each {|k| copy[k].delete(i)}
            copy.delete(j)
            copy.keys.each {|k| copy[k].delete(j)}
            m = f(color, copy)
            min = m if m < min
        end
    end

    $memo[tops] = min
end

loop do
    n = gets.to_i
    break if n == 0

    circles = []
    color = []
    n.times do
        x, y, r, c = gets.split.map(&:to_i)
        circles << [x, y, r]
        color << c
    end

    g = {}
    (0..n-1).each {|i| g[i] = Set.new}

    (0..n-1).to_a.combination(2) do |i, j|
        x1, y1, r1 = circles[i]
        x2, y2, r2 = circles[j]
        if Math.hypot(x1 - x2, y1 - y2) < r1 + r2
            g[j] << i
        end
    end
    
    $memo = {}
    p n - f(color, g)
end