Problem:
Problem G: Color the Map

You were lucky enough to get a map just before entering the legendary magical mystery world.
The map shows the whole area of your planned exploration, including several countries with
complicated borders. The map is clearly drawn, but in sepia ink only; it is hard to recognize at
a glance which region belongs to which country, and this might bring you into severe danger. You
have decided to color the map before entering the area. “A good deal depends on preparation,”
you talked to yourself.

Each country has one or more territories, each of which has a polygonal shape. Territories
belonging to one country may or may not “touch” each other, i.e. there may be disconnected
territories. All the territories belonging to the same country must be assigned the same color.
You can assign the same color to more than one country, but, to avoid confusion, two countries
“adjacent” to each other should be assigned different colors. Two countries are considered to be
“adjacent” if any of their territories share a border of non-zero length.

Write a program that finds the least number of colors required to color the map.

Input

The input consists of multiple map data. Each map data starts with a line containing the total
number of territories n, followed by the data for those territories. n is a positive integer not
more than 100. The data for a territory with m vertices has the following format:

String
x1 y1
x2 y2
...
xm ym
-1

“String” (a sequence of alphanumerical characters) gives the name of the country it belongs to.
A country name has at least one character and never has more than twenty. When a country
has multiple territories, its name appears in each of them.

Remaining lines represent the vertices of the territory. A vertex data line has a pair of nonneg-
ative integers which represent the x- and y-coordinates of a vertex. x- and y-coordinates are
separated by a single space, and y-coordinate is immediately followed by a newline. Edges of
the territory are obtained by connecting vertices given in two adjacent vertex data lines, and byconnecting vertices given in the last and the first vertex data lines. None of x- and y-coordinates
exceeds 1000. Finally, -1 in a line marks the end of vertex data lines. The number of vertices
m does not exceed 100.

You may assume that the contours of polygons are simple, i.e. they do not cross nor touch
themselves. No two polygons share a region of non-zero area. The number of countries in a map
does not exceed 10.

The last map data is followed by a line containing only a zero, marking the end of the input
data.

Output

For each map data, output one line containing the least possible number of colors required to
color the map satisfying the specified conditions.

Sample Input

6
Blizid
0 0
60 0
60 60
0 60
0 50
50 50
50 10
0 10
-1
Blizid
0 10
10 10
10 50
0 50
-1
Windom
10 10
50 10
40 20
20 20
20 40
10 50
-1
Accent
50 10
50 50
35 50
35 25
-1
Pilot
35 25
35 50
10 50
-1
Blizid
20 20
40 20
20 40
-1
4
A1234567890123456789
0 0
0 100
100 100
100 0
-1
B1234567890123456789
100 100
100 200
200 200
200 100
-1
C1234567890123456789
0 100
100 100
100 200
0 200
-1
D123456789012345678
100 0
100 100
200 100
200 0
-1
0

Output for the Sample Input

4
2

Solution in Ruby (judge verdict: Wrong Answer)

### 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