#include <iostream>
#include <iomanip>
#include <complex>
#include <vector>
#include <algorithm>
#include <cmath>
#include <array>
using namespace std;
typedef long long int lli;
const double EPS = 1e-6;
const double INF = 1e12;
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2>{
    L(const P& a, const P& b){ at(0)=a; at(1)=b; }
    L(){}
};

double dot(P a, P b){
    return (conj(a)*b).X;
}
double cross(P a, P b){
    return (conj(a)*b).Y;
}
int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > EPS) return +1; //ccw
    if(cross(b,c) < -EPS) return -1; //cw
    if(dot(b,c) < -EPS) return +2; //c-a-b
    if(abs(c)-abs(b) > EPS) return -2; //a-b-c
    return 0; //a-c-b
}

bool intersectSS(const L& a, const L& b){
    return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&
        ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );
}

vector<lli> makegraph(const vector<vector<L> > &l){
    int n = l.size();
    vector<lli> ret(n, 0);
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            bool touch = false;
            for(const L &e1: l[i]){
                for(const L &e2: l[j]){
                    if(intersectSS(e1, e2)){
                        touch = true;
                        break;
                    }
                }
            }
            if(touch){
                ret[i] |= 1LL<<j;
                ret[j] |= 1LL<<i;
            }
        }
    }
    return ret;
}

int bitcount(int n){
    n = (n & 0x55555555) + (n >> 1 & 0x55555555);
    n = (n & 0x33333333) + (n >> 2 & 0x33333333);
    n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
    n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
    return (n & 0x0000ffff) + (n >>16 & 0x0000ffff);
}

//入力はグラフのビット表現
int chromatic_number(const vector<lli> &g){
    int n = g.size();
    if(n == 0) return 0;
    lli mod = 1e9+7;
    vector<lli> ind(1LL<<n);
    ind[0] = 1;
    for(int i=1; i<(1LL<<n); i++){
        int pop;
        for(int j=0; j<n; j++){
            if((i & 1LL<<j) != 0){
                pop = j;
                break;
            }
        }
        ind[i] = ind[i -(1LL<<pop)] +ind[(i -(1LL<<pop)) & ~g[pop]];
        ind[i] %= mod;
    }  
    vector<lli> sign(1LL<<n), indpow = ind;
    for(lli i=0; i<(1LL<<n); i++){
        sign[i] = ((n -bitcount(i))%2 == 0)? 1: -1;
    }
    for(int k=1; k<n; k++){
        lli chro = 0;
        for(int i=0; i<(1LL<<n); i++){
            chro += (sign[i] *indpow[i]) %mod;
            indpow[i] = (indpow[i] *ind[i])%mod;
        }
        if(chro % mod != 0){
            return k;
        }
    }
    return n;
}

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;
        
        vector<VP> p(n);
        for(int i=0; i<n; i++){
            int m;
            cin >> m;
            p[i].resize(m);
            for(int j=0; j<m; j++){
                int x,y;
                cin >> x >> y;
                p[i][j] = P(x, y);
            }
        }
        vector<vector<L> > lines(n);
        for(int i=0; i<n; i++){
            int m = p[i].size();
            lines[i].resize(m-1);
            for(int j=0; j<m-1; j++){
                lines[i][j] = L(p[i][j], p[i][j+1]);
            }
        }
        vector<lli> g = makegraph(lines);
        cout << chromatic_number(g) << endl;
    }
    return 0;
}




Measured memory_bytes: 125628