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 <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <complex>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef double D;
typedef complex<D> P;
typedef pair<P,P> L;
typedef vector<P> Pol;
D inf=1e10,eps=1e-10;
int W,N,M[202];
D d[202][202];
Pol pol[202];
void init(){
	rep(i,N+2) pol[i].clear();
	rep(i,N+2) rep(j,N+2) d[i][j]=inf;
	rep(i,N+2) d[i][i]=0;
}
D dot(P a,P b){
	return real(conj(a)*b);
}
P perp(L l,P p){
	D t=dot(p-l.fs,l.fs-l.sc)/norm(l.fs-l.sc);
	return l.fs+t*(l.fs-l.sc);
}
bool iSP(L s,P p){
	D t=abs(s.fs-p)+abs(s.sc-p)-abs(s.fs-s.sc);
	return abs(t)<eps;
}
D dSP(L s,P p){
	P q=perp(s,p);
	return iSP(s,q) ? abs(p-q) : min(abs(p-s.fs),abs(p-s.sc));
}
D dSS(L a,L b){
	return min(min(dSP(a,b.fs),dSP(a,b.sc)),min(dSP(b,a.fs),dSP(b,a.sc)));
}
D dist(int a,int b){
	Pol x=pol[a],y=pol[b];
	D ret=inf;
	rep(i,x.size()){
		L p=L(x[i],x[(i+1)%x.size()]);
		rep(j,y.size()){
			L l=L(y[j],y[(j+1)%y.size()]);
			ret=min(ret,dSS(p,l));
		}
	}
	return ret;
}
int main(){
	while(true){
		cin>>W>>N;
		if(W==0) break;
		init();
		rep(i,N){
			cin>>M[i];
			rep(j,M[i]){
				int x,y;
				cin>>x>>y;
				pol[i].pb(P(x,y));
			}
		}
		pol[N].pb(P(0,0));
		pol[N].pb(P(0,10000));
		pol[N+1].pb(P(W,0));
		pol[N+1].pb(P(W,10000));
		M[N]=2,M[N+1]=2;
		N+=2;
		rep(i,N) rep(j,i){
			d[i][j]=d[j][i]=dist(i,j);
//			show(i);
//			show(j);
//			show(d[i][j]);
		}
		rep(i,N) rep(j,N) rep(k,N) d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
		printf("%.12f\n",d[N-2][N-1]);
	}
}