#include #include #include #include #include #define foreach(T,p,c) for(T p = (c).begin(); p != (c).end(); p++) using namespace std; //////////////////////////////////////////////////////////////////////////// class edge_t { private: int a, b, c, s, t; private: static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } public: edge_t(void) { } edge_t(int x0, int y0, int x1, int y1) { a = y1 - y0; b = x0 - x1; c = x1 * y0 - x0 * y1; if(a < 0 || (a == 0 && b < 0)) { a = -a; b = -b; c = -c; } int g = gcd(gcd(abs(a), abs(b)), abs(c)); a /= g; b /= g; c /= g; if(a == 0) { s = min(x0, x1); t = max(x0, x1); } else { s = min(y0, y1); t = max(y0, y1); } } public: bool overlap(const edge_t &other) { if(a != other.a || b != other.b || c != other.c) return false; if(s < other.s && other.s < t) return true; if(other.s < s && s < other.t) return true; if(s == other.s) return true; return false; } }; //////////////////////////////////////////////////////////////////////////// static bool adj[10][10]; static vector edges[10]; int color(int n, int k = 0, int c = 0, int c_min = INT_MAX) { static int painted[10]; if(c >= c_min) return c_min; if(k == n) return c; for(int i = 0; i < c; i++) { bool flag = true; for(int j = 0; j < k; j++) { if(painted[j] == i && adj[j][k]) { flag = false; break; } } if(flag) { painted[k] = i; c_min = color(n, k + 1, c, c_min); } } painted[k] = c; c_min = color(n, k + 1, c + 1, c_min); return c_min; } bool is_adjacent(int s, int t) { foreach(vector::iterator, p, edges[s]) { foreach(vector::iterator, q, edges[t]) { if(p->overlap(*q)) return true; } } return false; } int main(void) { ifstream cin("G.txt"); int n; while(cin >> n) { if(n == 0) break; for(int i = 0; i < 10; i++) { edges[i].clear(); } map countries; for(int i = 0; i < n; i++) { string s; cin >> s; countries.insert(make_pair(s, countries.size())); int k = countries[s]; int x0, y0; cin >> x0 >> y0; int ux, uy; ux = x0; uy = y0; while(true) { int vx, vy; cin >> vx; if(vx == -1) break; cin >> vy; edges[k].push_back(edge_t(ux, uy, vx, vy)); ux = vx; uy = vy; } edges[k].push_back(edge_t(ux, uy, x0, y0)); } int N = countries.size(); for(int i = 0; i < N; i++) { adj[i][i] = false; for(int j = i + 1; j < N; j++) { adj[i][j] = adj[j][i] = is_adjacent(i, j); } } cout << color(N) << endl; } return 0; }