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<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
int x[300][50];
int y[300][50];
double ijk[300];
int v[300];
int size[300];
const double EPS=1e-10;
const double INF=1e+10;
const double PI=acos(-1);
int sig(double r){return (r<-EPS)?-1:(r>+EPS)?+1:0;}
double Abs(double a){return max(a,-a);}
struct Pt{
	double x,y;
	Pt(){}
	Pt(double x,double y):x(x),y(y){}
	Pt operator+(const Pt &a)const{return Pt(x+a.x,y+a.y);}
	Pt operator-(const Pt &a)const{return Pt(x-a.x,y-a.y);}
	Pt operator*(const Pt &a)const{return Pt(x*a.x-y*a.y,x*a.y+y*a.x);}
	Pt operator-()const{return Pt(-x,-y);}
	Pt operator*(const double &k)const{return Pt(x*k,y*k);}
	Pt operator/(const double &k)const{return Pt(x/k,y/k);}
	double Abs()const{return sqrt(x*x+y*y);}
	double Abs2()const{return x*x+y*y;}
	double arg()const{return atan2(y,x);}
	double dot(const Pt &a)const{return x*a.x+y*a.y;}
	double det(const Pt &a)const{return x*a.y-y*a.x;}
};
Pt point[300][50];
double tri(const Pt&a,const Pt&b,const Pt&c){return (b-a).det(c-a);}
int iSP(Pt a,Pt b,Pt c){
	int s=sig((b-a).det(c-a));
	if(s)return s;
	if(sig((b-a).dot(c-a))<0)return -2;
	if(sig((a-b).dot(c-b))<0)return +2;
	return 0;
}
bool iSS(Pt a,Pt b,Pt c,Pt d){
	return (iSP(a,b,c)*iSP(a,b,d)<=0&&iSP(c,d,a)*iSP(c,d,b)<=0);
}
double dSP(Pt a,Pt b,Pt c){
	if(sig((b-a).dot(c-a))<=0)return (c-a).Abs();
	if(sig((a-b).dot(c-b))<=0)return (c-b).Abs();
	return Abs(tri(a,b,c))/(b-a).Abs();
}
double dSS(Pt a,Pt b,Pt c,Pt d){
	return iSS(a,b,c,d)?0:min(min(dSP(a,b,c),dSP(a,b,d)),min(dSP(c,d,a),dSP(c,d,b)));
}
double dist[300][300];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		for(int i=0;i<b;i++){
			int c;
			scanf("%d",&c);
			size[i]=c;
			for(int j=0;j<c;j++){
				scanf("%d%d",&y[i][j],&x[i][j]);
				point[i][j]=Pt(x[i][j],y[i][j]);
			}
		}
		for(int i=0;i<300;i++){
			for(int j=0;j<300;j++){
				if(i==j)dist[i][j]=0;
				else dist[i][j]=999999999;
			}
		}
		dist[b][b+1]=a;
		for(int i=0;i<b;i++){
			for(int j=0;j<size[i];j++){
				dist[b][i]=dist[i][b]=min(dist[b][i],point[i][j].y);
				dist[b+1][i]=dist[i][b+1]=min(dist[b+1][i],a-point[i][j].y);
			}
		}
		for(int i=0;i<b;i++){
			for(int j=i+1;j<b;j++){
				for(int k=0;k<size[i];k++)
					for(int l=0;l<size[j];l++)
						dist[i][j]=dist[j][i]=min(dist[i][j],dSS(point[i][k],point[i][(k+1)%size[i]],point[j][l],point[j][(l+1)%size[j]]));
			}
		}
		for(int k=0;k<b+2;k++)
			for(int i=0;i<b+2;i++)
				for(int j=0;j<b+2;j++)
					dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
		printf("%.8f\n",dist[b][b+1]);
	}
}