#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Point
{
public:
    int y, x;
    Point(){
        y = x = 0;
    }
    Point(int y0, int x0){
        y = y0;
        x = x0;
    }
    Point operator+(const Point& p) const{
        return Point(y + p.y, x + p.x);
    }
    Point operator-(const Point& p) const{
        return Point(y - p.y, x - p.x);
    }
    Point operator*(int a) const{
        return Point(y * a, x * a);
    }
    int dot(const Point& p) const{
        return y * p.y + x * p.x; // |a|*|b|*cos慮
    }
    int cross(const Point& p) const{
        return x * p.y - y * p.x; // |a|*|b|*sin慮
    }
    bool operator==(const Point& p) const{
        return x == p.x && y == p.y;
    }
};

bool segmentsCollide(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
    if(min(a1.x, a2.x) > max(b1.x, b2.x) || min(b1.x, b2.x) > max(a1.x, a2.x) || min(a1.y, a2.y) > max(b1.y, b2.y) || min(b1.y, b2.y) > max(a1.y, a2.y))
        return false;

    return (a2-a1).cross(b1-a1) == 0 && (a2-a1).cross(b2-a1) == 0 &&
                ((b1-a1).dot(b2-a1) < 0 || (b1-a2).dot(b2-a2) < 0 || (a1-b1).dot(a2-b1) < 0 || (a1-b2).dot(a2-b2) < 0 || (a1 == b1 && a2 == b2) || (a1 == b2 && a2 == b1));
}

bool polygonsCollide(const vector<Point>& cp1, const vector<Point>& cp2)
{
    int n = cp1.size();
    int m = cp2.size();
    for(int i=0; i<n; ++i){
        for(int j=0; j<m; ++j){
            if(segmentsCollide(cp1[i], cp1[(i+1)%n], cp2[j], cp2[(j+1)%m]))
                return true;
        }
    }
    return false;
}

int solve(vector<vector<bool> >& connect, vector<int>& color, int colorNum, int i)
{
    int n = color.size();
    if(i == n)
        return colorNum;

    int ret = INT_MAX;
    for(int j=0; j<colorNum; ++j){
        bool ok = true;
        for(int k=0; k<i; ++k){
            if(connect[i][k] && color[k] == j)
                ok = false;
        }

        if(ok){
            color[i] = j;
            ret = min(ret, solve(connect, color, colorNum, i+1));
        }
    }

    color[i] = colorNum;
    ret = min(ret, solve(connect, color, colorNum+1, i+1));

    return ret;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<string> name(n);
        vector<vector<Point> > p(n);
        int m = 0;
        map<string, int> index;
        for(int i=0; i<n; ++i){
            cin >> name[i];
            if(index.find(name[i]) == index.end()){
                index[name[i]] = m;
                ++ m;
            }
            for(;;){
                int x, y;
                cin >> x;
                if(x == -1)
                    break;
                cin >> y;
                p[i].push_back(Point(y, x));
            }
        }

        vector<vector<bool > > connect(m, vector<bool>(m, false));
        for(int i=0; i<n; ++i){
            for(int j=i+1; j<n; ++j){
                if(index[name[i]] != index[name[j]] && polygonsCollide(p[i], p[j]))
                    connect[index[name[i]]][index[name[j]]] = connect[index[name[j]]][index[name[i]]] = true;
            }
        }

        vector<int> color(m);
        cout << solve(connect, color, 0, 0) << endl;
    }
}

Measured memory_bytes: 956