### constant

MAX_C = 4

### subroutines

def overwrap?(dsk0, dsk1)
  x0, y0, r0 = dsk0
  x1, y1, r1 = dsk1
  (x1 - x0) ** 2 + (y1 - y0) ** 2 < (r0 + r1) ** 2
end

def count_rec(n, clrs, ovs, used)
  max_cnt = 0

  
  max_cnt
end

### main

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

  dsks = []
  clrs = MAX_C.times.map{[]}
  ovs = n.times.map{0}
  
  for i in (0...n)
    xi, yi, ri, ci = gets.split.map(&:to_i)
    ci -= 1
    dsk = [xi, yi, ri, ci]
    dsks << dsk
    clrs[ci] << i

    for j in (0...i)
      dskj = dsks[j]
      if overwrap?(dskj, dsk)
        ovs[i] |= (1 << j)
      end
    end
  end
  #p dsks
  #p clrs
  #p ovs

  max_cnt = 0
  st = (1 << n) - 1
  dists = (0..st).map{-1}
  dists[st] = 0
  q = [st]

  while ! q.empty?
    ubits = q.shift
    ud = dists[ubits]

    max_cnt = ud if max_cnt < ud

    nvd = ud + 2
    
    for ids in clrs
      len = ids.length
      for cid0 in (0...len)
        id0 = ids[cid0]
        bit0 = (1 << id0)
        next if (ubits & bit0) == 0 || (ubits & ovs[id0]) != 0

        for cid1 in ((cid0 + 1)...len)
          id1 = ids[cid1]
          bit1 = (1 << id1)
          next if (ubits & bit1) == 0 || (ubits & ovs[id1]) != 0

          vbits = ubits & ~(bit0 | bit1)
          if dists[vbits] < 0
            dists[vbits] = nvd
            q << vbits
          end
        end
      end
    end
  end

  puts max_cnt
end