#include <bits/stdc++.h>
using namespace std;


using ld = long double;

constexpr ld eps = 1e-8;


namespace geometry2d {


using point = std::complex<long double>;
using polygon = std::vector<point>;


bool eq(long double a, long double b) {
    return (std::abs(a-b) < eps);
}


long double dot(point a, point b) {
    return std::real(std::conj(a) * b);
}

long double cross(point a, point b) {
    return std::imag(std::conj(a) * b);
}


class segment {
public:
    segment()
        : a(point(0, 0)),
          b(point(0, 0))
    {}
    segment(point a_, point b_)
        : a(a_), b(b_)
    {}

    point a, b;
};

class line {
public:
    line()
        : a(point(0, 0)), b(point(0, 0))
    {}
    line(point a_, point b_)
        : a(a_), b(b_)
    {}
    line(segment s)
        : a(s.a), b(s.b)
    {}

    point a, b;
};

int ccw(point a, point b, point c) {
    b -= a; c -= a;
    if(cross(b, c) > eps)           return 1;  // a -> b -> c : counterclockwise
    if(cross(b, c) < -eps)          return -1; // a -> b -> c : clockwise
    if(dot(b, c) < 0)               return 2;  // c -> a -> b : line
    if(std::norm(b) < std::norm(c)) return -2; // a -> b -> c : line
    return 0;                                  // a -> c -> b : line
}

bool isis_ll(line l, line m) {
    return !eq(cross(l.b - l.a, m.b - m.a), 0);
}

bool isis_ls(line l, segment s) {
    return (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - s.a) < eps);
}

bool isis_sp(segment s, point p) {
    return (std::abs(s.a - p) + std::abs(s.b - p) - std::abs(s.b - s.a) < eps);
}

bool isis_ss(segment s, segment t) {
    return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0
        && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}


point proj(line l, point p) {
    long double t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
    return l.a + t * (l.a - l.b);
}

long double dist_sp(segment s, point p) {
    point r = proj(line(s), p);
    return isis_sp(s, r) ? std::abs(r - p) : std::min(std::abs(s.a - p), std::abs(s.b - p));
}

long double dist_ss(segment s, segment t) {
    if(isis_ss(s, t)) return 0;
    long double d1 = std::min(dist_sp(s, t.a), dist_sp(s, t.b));
    long double d2 = std::min(dist_sp(t, s.a), dist_sp(t, s.b));
    return std::min(d1, d2);
}


} // namespace geometry2d

using namespace geometry2d;

ld slope(segment s) {
    if(abs(real(s.a) - real(s.b)) < eps) {
        return 1e18;
    }
    return abs((imag(s.a) - imag(s.b)) / (real(s.a) - real(s.b)));
}

bool is_on(segment s1, segment s2) {
    if(dist_ss(s1, s2) > eps) {
        return false;
    }
    if(abs(slope(s1) - slope(s2)) > eps) {
        return false;
    }
    vector<pair<point, int>> v;
    v.emplace_back(s1.a, 0); v.emplace_back(s1.b, 0);
    v.emplace_back(s2.a, 1); v.emplace_back(s2.b, 1);
    sort(v.begin(), v.end(), [](auto const& p1, auto const& p2) {
        if(abs(real(p1.first) - real(p2.first)) < eps) {
            if(abs(imag(p1.first) - imag(p2.first)) < eps) {
                return p1.second < p2.second;
            }
            return imag(p1.first) < imag(p2.first);
        }
        return real(p1.first) < real(p2.first);
    });
    if(abs(v[1].first - v[2].first) < eps) {
        return false;
    }
    return true;
}


void solve(vector<int>& color, vector<vector<bool>> const& f, int i, int k, int& res) {
    const int N = color.size();
    if(k >= res) {
        return;
    }
    if(i == N) {
        res = k;
        return;
    }
    vector<bool> used(N);
    for(int j=0; j<N; ++j) {
        if(color[j] != -1 && f[i][j]) {
            used[color[j]] = true;
        }
    }
    for(int j=0; j<k+1; ++j) {
        if(!used[j]) {
            color[i] = j;
            solve(color, f, i+1, max(j+1, k), res);
        }
    }
    color[i] = -1;
}

int main() {
    int n;
    while(cin >> n, n) {
        map<string, int> id;
        vector<int> ter_id(n);
        vector<polygon> ter(n);
        for(int i=0; i<n; ++i) {
            string s;
            cin >> s;
            if(id.count(s) == 0) {
                id[s] = id.size()-1;
            }
            ter_id[i] = id[s];
            int x, y;
            polygon p;
            while(cin >> x, x != -1) {
                cin >> y;
                p.emplace_back(ld(x), ld(y));
            }
            ter[i] = p;
        }
        int res = 10;
        vector<vector<bool>> f(id.size(), vector<bool>(id.size()));
        for(int i=0; i<id.size(); ++i) {
            f[i][i] = true;
        }
        vector<int> color(id.size(), -1);
        for(int i=0; i<ter.size(); ++i) {
            for(int j=i+1; j<ter.size(); ++j) {
                bool connect = false;
                for(int k=0; k<ter[i].size(); ++k) {
                    segment s1(ter[i][k], ter[i][(k+1)%ter[i].size()]);
                    for(int l=0; l<ter[j].size(); ++l) {
                        segment s2(ter[j][l], ter[j][(l+1)%ter[j].size()]);
                        connect |= is_on(s1, s2);
                    }
                }
                f[ter_id[i]][ter_id[j]] = f[ter_id[i]][ter_id[j]] | connect;
                f[ter_id[j]][ter_id[i]] = f[ter_id[j]][ter_id[i]] | connect;
            }
        }
        solve(color, f, 0, 1, res);
        cout << res << endl;
    }
}


Measured memory_bytes: 3252