require 'set'

class Line
	#ax+by = c   (a > 0 or (a == 0 && b > 0))
	def self.through_two_points(x1, y1, x2, y2)
		a = y1 - y2
		b = x2 - x1
		c = a * x1 + b * y1
		new(a, b, c, x1, y1, x2, y2)
	end

	def overlap?(l)
		if @a == 0
			return false unless l.a == 0
			return false unless l.b * @c == @b * l.c
		else
			return false unless l.a * @b == @a * l.b && l.a * @c + @a * l.c
		end
		if @a == 0
			return true if @x1 <= l.x1 && l.x1 < @x2
			return true if @x1 < l.x2 && l.x2 <= @x2
			return true if l.x1 <= @x1 && @x2 <= l.x2
			return true if @x1 < l.x1 && l.x2 < @x2
		else
			return true if @y1 <= l.y1 && l.y1 < @y2
			return true if @y1 < l.y2 && l.y2 <= @y2
			return true if l.y1 <= @y1 && @y2 <= l.y2
			return true if @y1 <= l.y1 && l.y2 <= @y2
		end
		false
	end

	private_class_method :new

	def initialize(*args)
		@a, @b, @c, @x1, @y1, @x2, @y2 = args	
		@x1, @y1, @x2, @y2 = @x2, @y2, @x1, @y1 if @y1 > @y2
		@x1, @y1, @x2, @y2 = @x2, @y2, @x1, @y1 if @x1 > @x2
		if @a < 0
			@a *= -1
			@b *= -1
			@c *= -1
		elsif @a == 0 && @b < 0
			@b *= -1
			@c *= -1
		end
	end

	attr_reader :a, :b, :c, :x1, :y1, :x2, :y2
end

def f(graph, color)
	return color.values.max if color.size == graph.size
	i = color.size
	candidates = (1..color.values.max+1).to_a - graph[i].map{|j| color[j]}
	min = Float::INFINITY
	candidates.each do |c|
		new_color = color.dup
		new_color[i] = c
		x = f(graph, new_color)
		min = x if x < min
	end
	min
end

loop do
	n = gets.to_i
	break if n == 0
	territory = []
	country = {}
	name_to_idx = {}
	n.times do
		name = gets.chomp
		c = (name_to_idx[name] ||= name_to_idx.size)
		pos = []
		loop do
			x, y = gets.split.map(&:to_i)
			break if x == -1
			pos << [x, y]
		end
		pos << pos[0]
		lines = pos.each_cons(2).map do |p1, p2|
			Line::through_two_points(*p1, *p2)
		end
		country[territory.size] = c
		territory << lines
	end

	t_edges = []
	(0..territory.size-1).to_a.combination(2) do |i, j|
		t1, t2 = territory[i], territory[j]
		if t1.product(t2).any?{|l1, l2| l1.overlap?(l2)}
			t_edges << [i, j]
		end
	end
	
	graph = {}
	(0..name_to_idx.size-1).each {|i| graph[i] = Set.new}
	t_edges.each do |i, j|
		u = country[i]
		v = country[j]
		graph[u] << v
		graph[v] << u
	end

	p f(graph, {0 => 1})
end