#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;
typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};

typedef vector<P> G;

int ccw(P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > 0)   return +1;       // counter clockwise
  if (cross(b, c) < 0)   return -1;       // clockwise
  if (dot(b, c) < 0)     return +2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;       // a--b--c on line
  return 0;
}

bool intersectSS(const L &s, const L &t) {
  REP(i, 2) {
    if (ccw(s[0], s[1], t[i]) == 0) {
      int c = ccw(s[0],s[1],t[!i]);
      if (s[0] == t[i]) {
        if (c!=-2&&c) return 0;
      } else if (s[1] == t[i]) {
        if (c!=2&&c) return 0;
      } else if (abs(c)==1) return 0;
    }
  }
  return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
         ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}

bool intersectGG(G a, G b) {
  a.push_back(a[0]);
  b.push_back(b[0]);
  REP(i, a.size()-1)
    REP(j, b.size()-1)
      if (intersectSS(L(a[i], a[i+1]), L(b[j], b[j+1])))
        return 1;
  return 0;
}

int n;
int num;

vector<int> graph[10];
int color[10];

int dfs(int id, int maxcolor) {
  if (id == num) return maxcolor;
  int res = INF;
  for (int i=1; i<=maxcolor; ++i) {
    bool dame = 0;
    FOR(it, graph[id])
      if (color[*it] == i)
        goto NEXT;
    color[id] = i;
    res = min(res, dfs(id+1, maxcolor));
  NEXT:;
  }
  color[id] = maxcolor+1;
  res = min(res, dfs(id+1, maxcolor+1));
  return res;
}

int main() {
  // cout << "0 -> " << intersectSS(L(P(100,100), P(100,0)), L(P(100,90), P(200,200))) << endl;
  // cout << "1 -> " << intersectSS(L(P(100,100), P(100,0)), L(P(100,90), P(100,200))) << endl;
  // cout << "1 -> " << intersectSS(L(P(100,100), P(100,0)), L(P(100,100), P(100,0))) << endl;
  // return 0;
  while(cin >> n, n) {
    vector<G> polygon[n];
    num = 0;
    map<string, int> id;
    REP(i, n) {
      string name;
      cin >> name;
      int x, y;
      G tmp;
      while(cin >> x, x!=-1) {
        cin >> y;
        tmp.push_back(P(x,y));
      }
      if (id.count(name) == 0) id[name] = num++;
      polygon[id[name]].push_back(tmp);
    }
    REP(i, num) graph[i].clear();
    REP(i, num) {
      REP(j, i) {
        FOR(it, polygon[i]) {
          FOR(jt, polygon[j]) {
            if (intersectGG(*it, *jt)) {
              graph[i].push_back(j);
              graph[j].push_back(i);
              goto NEXT;
            }
          }
        }
      NEXT:;
      }
    }
    // REP(i, num) {
    //   cout << i << " : ";
    //   FOR(it, graph[i])
    //     cout << *it << " ";
    //   cout << endl;
    // }
    memset(color, 0, sizeof(color));
    cout << dfs(0, 0) << endl;
  }
}

Measured memory_bytes: 984