#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

#define EPS (1e-10)
#define eq(a, b) (fabs((a)-(b)) < EPS)
#define lt(a, b) ((a) - (b) < -EPS)
#define le(a, b) (eq(a, b) || lt(a, b))

struct Point {
  double x, y;
  Point(double x = 0.0, double y = 0.0):x(x), y(y){}

  Point operator + (Point p) { return Point(x + p.x, y + p.y); }
  Point operator - (Point p) { return Point(x - p.x, y - p.y); }
  Point operator * (double a) { return Point(x * a, y * a); }
  Point operator / (double a) { return Point(x / a, y / a); }

  bool operator < (const Point& p) const {
    return x != p.x ? x < p.x : y < p.y;
  }
  bool operator == (const Point& p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

double dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; }
double norm(Vector v) { return dot(v, v); }
double abs(Vector v) { return sqrt(norm(v)); }
double cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; }

bool isParallel(Vector a, Vector b) {
  return eq(cross(a, b), 0.0);
}
bool isParallel(Point a1, Point a2, Point b1, Point b2) {
  return isParallel(a1 - a2, b1 - b2);
}

#define COUNTER_CLOCKWISE +1
#define CLOCKWISE         -1
#define ONLINE_BACK       +2
#define ONLINE_FRONT      -2
#define ON_SEGMENT        +0
int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if(cross(a, b) > EPS) return COUNTER_CLOCKWISE;
  if(cross(a, b) < -EPS) return CLOCKWISE;
  if(dot(a, b) < -EPS) return ONLINE_BACK;
  if(norm(a) < norm(b)) return ONLINE_FRONT;
  return ON_SEGMENT;
}
bool intersect(Point p1, Point p2, Point p3, Point p4) {
  return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
	  ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}

struct Segment {
  Point p1, p2;
  Segment(Point p1 = Point(), Point p2 = Point()):p1(p1), p2(p2){}
};
typedef Segment Line;

bool isParallel(Segment s1, Segment s2) {
  return eq(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0);
}

typedef vector<Point> Polygon;

int n;
vector<string> name;
map<string, int> mp;
vector<int> naid;
vector<Polygon> teri;
vector< vector<Segment> > seg;
vector<int> col;
bool adj[11][11];

bool check(int a, int b) {
  for(auto s1 : seg[a]) {
    for(auto s2 : seg[b]) {
      if(isParallel(s1, s2)) {
	if((s1.p1 == s2.p1 && s1.p2 == s2.p2) ||
	   (s1.p1 == s2.p2 && s1.p2 == s2.p1)) {
	  return true;
	}
	if(ccw(s1.p1, s1.p2, s2.p1) == ON_SEGMENT &&
	   !(s1.p1 == s2.p1 || s1.p2 == s2.p1)) {
	  return true;
	}
	if(ccw(s1.p1, s1.p2, s2.p2) == ON_SEGMENT &&
	   !(s1.p1 == s2.p2 || s1.p2 == s2.p2)) {
	  return true;
	}
	if(ccw(s2.p1, s2.p2, s1.p1) == ON_SEGMENT &&
	   !(s2.p1 == s1.p1 || s2.p2 == s1.p1)) {
	  return true;
	}
	if(ccw(s2.p1, s2.p2, s1.p2) == ON_SEGMENT &&
	   !(s2.p1 == s1.p2 || s2.p2 == s1.p2)) {
	  return true;
	}
      }
    }
  }
  return false;
}

int N;
int ans;

void solve(int idx, int cnum) {
  if(cnum >= ans) return;
  if(idx == N) {
    chmin(ans, cnum);
    return;
  }
  set<int> adjcol;
  rep(i, N) {
    if(adj[idx][i] && ~col[i]) adjcol.insert(col[i]);
  }
  rep(i, cnum) {
    if(!adjcol.count(i)) {
      col[idx] = i;
      solve(idx+1, cnum);
      col[idx] = -1;
    }
  }
  col[idx] = cnum;
  solve(idx+1, cnum+1);
  col[idx] = -1;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while(cin >> n, n) {
    resz(name, n);
    mp.clear();
    resz(naid, n);
    resz(teri, n);
    resz(seg, n);
    int num = 0;
    rep(i, n) {
      cin >> name[i];
      if(!mp.count(name[i])) mp[name[i]] = num++;
      naid[i] = mp[name[i]];
      int x, y;
      while(cin >> x, x != -1) {
	cin >> y;
	teri[i].push_back(Point(x, y));
      }
      int sz = teri[i].size();
      rep(j, sz) {
	seg[i].push_back(Segment(teri[i][j], teri[i][(j+1)%sz]));
      }
    }
    memset(adj, false, sizeof(adj));
    rep(i, n) reps(j, i+1, n) {
      if(naid[i] == naid[j]) continue;
      if(check(i, j)) adj[naid[i]][naid[j]] = adj[naid[j]][naid[i]] = true;
    }
    /*
    for(auto p : mp) {
      cout<<"<"<<p.first<<">"<<endl;
      int id = p.second;
      for(auto q : mp) {
	if(adj[id][q.second]) cout << q.first << " ";
      }
      cout<<endl;
    }
    */
    N = mp.size();
    resz(col, N, -1);
    ans = N;
    solve(0, 0);
    cout << ans << endl;
  }

  return 0;

}

Measured memory_bytes: 3248