Problem: Wind Corridor is a covered passageway where strong wind is always blowing. It is a long corridor of width W, and there are several pillars in it. Each pillar is a right prism and its face is a polygon (not necessarily convex).

In this problem, we consider two-dimensional space where the positive x-axis points the east and the positive y-axis points the north. The passageway spans from the south to the north, and its length is infinity. Specifically, it covers the area 0 ≤ x ≤ W. The outside of the passageway is filled with walls. Each pillar is expressed as a polygon, and all the pillars are located within the corridor without conflicting or touching each other.

Wind blows from the south side of the corridor to the north. For each second, w unit volume of air can be flowed at most if the minimum width of the path of the wind is w. Note that the path may fork and merge, but never overlaps with pillars and walls.

Your task in this problem is to write a program that calculates the maximum amount of air that can be flowed through the corridor per second.



Input

The input consists of multiple datasets. Each dataset has the following format:

The first line of the input contains two integers W and N. W is the width of the corridor, and N is the number of pillars. W and N satisfy the following condition: 1 ≤ W ≤ 104 and 0 ≤ N ≤ 200.

Then, N specifications of each pillar follow. Each specification starts with a line that contains a single integer M, which is the number of the vertices of a polygon (3 ≤ M ≤ 40). The following M lines describe the shape of the polygon. The i-th line (1 ≤ i ≤ M) contains two integers xi and yi that denote the coordinate of the i-th vertex (0 < xi < W, 0 < yi < 104).

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, your program should print a line that contains the maximum amount of air flow per second, in unit volume. The output may contain arbitrary number of digits after the decimal point, but the absolute error must not exceed 10-6.

Example

Input

5 2
4
1 1
1 2
2 2
2 1
4
3 3
3 4
4 4
4 3
0 0


Output

3.41421356

Solution (in CPP):

Solution:
#include<cmath>
#include<cstdio>
#include<vector>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const double EPS=1e-8;
const double INF=1e77;

template<class T>
struct point{
	T x,y;
	point operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }
};

template<class T>
struct segment{ point<T> a,b; };

template<class T>
struct polygon:vector< point<T> >{};

template<class T>
T dot(const point<T> &a,const point<T> &b){ return a.x*b.x+a.y*b.y; }

template<class T>
T cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }

enum{CCW=1,CW=-1,ON=0};
int ccw(const point<double> &a,const point<double> &b,const point<double> &c){
	double rdir=cross(b-a,c-a);
	if(rdir> EPS) return CCW;
	if(rdir<-EPS) return CW;
	return ON;
}

bool intersect(const segment<double> &S1,const segment<double> &S2){
	if(max(S1.a.x,S1.b.x)+EPS<min(S2.a.x,S2.b.x)
	|| max(S1.a.y,S1.b.y)+EPS<min(S2.a.y,S2.b.y)
	|| max(S2.a.x,S2.b.x)+EPS<min(S1.a.x,S1.b.x)
	|| max(S2.a.y,S2.b.y)+EPS<min(S1.a.y,S1.b.y)) return false;
	return ccw(S1.a,S1.b,S2.a)*ccw(S1.a,S1.b,S2.b)<=0
		&& ccw(S2.a,S2.b,S1.a)*ccw(S2.a,S2.b,S1.b)<=0;
}

template<class T>
T dist2(const point<T> &a,const point<T> &b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

template<class T>
double dist2(const segment<T> &S,const point<T> &p){
	if(dot(S.b-S.a,p-S.a)<=0) return dist2(p,S.a);
	if(dot(S.a-S.b,p-S.b)<=0) return dist2(p,S.b);
	return (double)cross(S.b-S.a,p-S.a)*cross(S.b-S.a,p-S.a)/dist2(S.a,S.b);
}

template<class T>
double dist(const segment<T> &S1,const segment<T> &S2){
	if(intersect(S1,S2)) return 0;
	return sqrt(min(min(dist2(S1,S2.a),dist2(S1,S2.b)),
					min(dist2(S2,S1.a),dist2(S2,S1.b))));
}

double dist(const polygon<double> &F1,const polygon<double> &F2){
	double res=INF;
	rep(i,F1.size()) rep(j,F2.size()) res=min(res,dist((segment<double>){F1[i],F1[(i+1)%F1.size()]},(segment<double>){F2[j],F2[(j+1)%F2.size()]}));
	return res;
}

int main(){
	for(int W,n;scanf("%d%d",&W,&n),W;){
		polygon<double> F[200];
		rep(i,n){
			int m; scanf("%d",&m);
			F[i].resize(m);
			rep(j,m) scanf("%lf%lf",&F[i][j].x,&F[i][j].y);
		}

		int s=n,t=n+1;
		double d[202][202];
		rep(u,n+2) rep(v,n+2) d[u][v]=(u==v?0:INF);
		// source -> sink
		d[s][t]=W;
		rep(u,n){
			// source -> node
			rep(i,F[u].size()) d[s][u]=min(d[s][u],F[u][i].x);
			// node -> node
			rep(v,n) d[u][v]=dist(F[u],F[v]);
			// node -> sink
			rep(i,F[u].size()) d[u][t]=min(d[u][t],W-F[u][i].x);
		}
		rep(w,n+2) rep(u,n+2) rep(v,n+2) d[u][v]=min(d[u][v],d[u][w]+d[w][v]);

		printf("%.9f\n",d[s][t]);
	}

	return 0;
}