Problem: You are an officer of the Department of Land and Transport in Oykot City. The department has a plan to build a subway network in the city central of Oykot.

In the plan, n subway lines are built, and each line has two or more stations. Because of technical problems, a rail track between two stations should be straight and should not have any slope. To make things worse, no rail track can contact with another rail track, even on a station. In other words, two subways on the same floor cannot have any intersection.

Your job is to calculate the least number of required floors in their plan.



Input

The input consists of multiple datasets. Each dataset is formatted as follows:


N
Line1
Line2
...
LineN


Here, N is a positive integer that indicates the number of subway lines to be built in the plan (N ≤ 22), and Linei is the description of the i-th subway line with the following format:


S
X1 Y1
X2 Y2
...
XS YS


S is a positive integer that indicates the number of stations in the line (S ≤ 30), and (Xi, Yi) indicates the coordinates of the i-th station of the line (-10000 ≤ Xi, Yi ≤ 10000). The rail tracks are going to be built between two consecutive stations in the description. No stations of the same line have the same coordinates.

The input is terminated by a dataset of N = 0, and it should not be processed.

Output

For each dataset, you should output the least number of required floors.

Example

Input

2
2
0 0
10 0
2
0 10
10 10
2
2
0 0
10 10
2
0 10
10 0
3
2
0 0
10 10
2
0 10
10 0
2
1 0
1 10
0


Output

1
2
3

Solution (in CPP):

Solution:
#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;
}