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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<int(n);++i)
using namespace std;

template<class T>bool chmin(T &a, const T &b) {return (b<a)?(a=b,1):0;}

using R=long double;
const R EPS = 1E-8; 
inline int sgn(const R& r){ return (r > EPS) - (r < -EPS);}

using P=complex<R>;
using L=struct{P s,t;};

inline R dot(P o,P a,P b){a-=o,b-=o; return real(conj(a)*b);}
inline R det(P o,P a,P b){a-=o,b-=o; return imag(conj(a)*b);}

R dsp(L s,P p){
	if(sgn(dot(s.s,s.t,p))<=0) return abs(p-s.s);
	if(sgn(dot(s.t,s.s,p))<=0) return abs(p-s.t);
	return abs(det(s.s,s.t,p))/abs(s.t-s.s);
}

R dss(L a,L b){return min({dsp(a,b.s),dsp(a,b.t),dsp(b,a.s),dsp(b,a.t)});}

const R inf=1e60;

int main(void){
	int w,m;
	cout.precision(20);
	while(cin >> w >> m,w){
		vector<L> pol[210];

		rep(i,m){
			int n,x[55],y[55];
			cin >> n;
			rep(j,n) cin >> x[j] >> y[j];
			rep(j,n) pol[i].push_back({P(x[j],y[j]),P(x[(j+1)%n],y[(j+1)%n])});
		}

		const int s=m,t=m+1,n=m+2;
		pol[s].push_back({P(0,-1e5),P(0,1e5)});
		pol[t].push_back({P(w,-1e5),P(w,1e5)});
		
		R dist[210][210];
		rep(i,n) dist[i][i]=0.0;

		rep(j,n)rep(i,j){
			R d=inf;
			for(auto &il:pol[i])
				for(auto &jl:pol[j]) 
					chmin(d,dss(il,jl));
			dist[i][j]=dist[j][i]=d;
		}
	
		rep(k,n)rep(i,n)rep(j,n) chmin(dist[i][j],dist[i][k]+dist[k][j]);
		cout << fixed << dist[s][t] << endl;
 	}
	return 0;
}