### subroutines

def vsub(v0, v1)
  [v0[0] - v1[0], v0[1] - v1[1]]
end

def dotp(v0, v1)
  v0[0] * v1[0] + v0[1] * v1[1]
end

def crossp(v0, v1)
  v0[0] * v1[1] - v0[1] * v1[0]
end

def overwrap?(p0, p1, p2, p3)
  v01 = vsub(p1, p0)
  v23 = vsub(p3, p2)
  return false if crossp(v01, v23) != 0

  v02 = vsub(p2, p0)
  return false if crossp(v01, v02) != 0

  d023 = dotp(vsub(p2, p0), vsub(p3, p0))
  d123 = dotp(vsub(p2, p1), vsub(p3, p1))
  d201 = dotp(vsub(p0, p2), vsub(p1, p2))
  d301 = dotp(vsub(p0, p3), vsub(p1, p3))

  if d023 < 0 || d123 < 0 || d201 < 0 || d301 < 0 ||
      (d023 == 0 && d123 == 0 && d201 == 0 && d301 == 0)
    #p ["adj?", p0, p1, p2, p3, d023, d123, d201, d301]
    return true
  end

  false
end

def adj?(ces0, ces1)
  ces0.each do |ce0|
    ces1.each do |ce1|
      return true if overwrap?(*ce0, *ce1)
    end
  end
  false
end

def rec(k, nc)
  if k >= $ncids
    $minc = nc if $minc > nc
    return
  end

  for c in (0..nc)
    ng = false
    for u in $nbrs[k]
      cu = $cols[u]
      if cu && cu == c
        ng = true
        break
      end
    end
    next if ng

    nc0 = (c == nc) ? nc + 1 : nc
    next if nc0 > $minc

    $cols[k] = c
    rec(k + 1, nc0)
    $cols[k] = nil
  end
end

### main

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

  cids = {}
  $ncids = 0
  cedges = []

  n.times do
    cname = gets.strip
    if cids[cname].nil?
      cids[cname] = $ncids
      cedges[$ncids] = []
      $ncids += 1
    end

    pts = []
    loop do
      line =gets.strip
      break if line == '-1'
      pts << line.split.map(&:to_i)
    end
    pts << pts.first

    ces = cedges[cids[cname]]
    pts.each_cons(2) do |p0, p1|
      ces << [p0, p1]
    end
  end
  #p cids
  #p cedges

  $nbrs = $ncids.times.map{[]}

  for i in (0...$ncids)
    cedgesi = cedges[i]
    for j in ((i + 1)...$ncids)
      cedgesj = cedges[j]
      if adj?(cedgesi, cedgesj)
        #p ["adj?", i, j]
        #$nbrs[i] << j
        $nbrs[j] << i
      end
    end
  end
  #p $nbrs

  $cols = [nil] * $ncids
  $minc = 4

  rec(0, 0)

  puts $minc
end