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 <complex>
#include <vector>

using namespace std;

const double EPS = 1e-7;
typedef complex<double> P;
typedef vector<P> G;
struct L { P p, q; L(P p, P q) : p(p), q(q) {} };
double dot(P a, P b) { return real(conj(a)*b); }
double cross(P a, P b) { return imag(conj(a)*b); }

double distLP(L l, P p){
	if(dot(l.q-l.p,p-l.p)<-EPS) return abs(p-l.p);
	if(dot(l.p-l.q,p-l.q)<-EPS) return abs(p-l.q);
	return abs(cross(l.q-l.p,p-l.p))/abs(l.q-l.p);
}

double distLL(L l, L m){
	double res = distLP(l, m.p);
	res = min(res, distLP(l, m.q));
	res = min(res, distLP(m, l.p));
	res = min(res, distLP(m, l.q));
	return res;
}

double distCC(G g1, G g2){
	double res = 1e12;
	for(int i=0;i<g1.size();i++){
		for(int j=0;j<g2.size();j++){
			res = min(res, distLL(L(g1[i],g1[(i+1)%g1.size()]),L(g2[j],g2[(j+1)%g2.size()])));
		}
	}
	return res;
}

int main(){
	double d1[200], d2[200];
	double dist[200][200];
	int w, n;
	while(cin >> w >> n, w){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++) dist[i][j] = 1e12;
			dist[i][i] = 0.0;
		}
		vector<G> vg;
		G vd1, vd2;
		vd1.push_back(P(0,-100));
		vd1.push_back(P(0,20000));
		vd2.push_back(P(w,-100));
		vd2.push_back(P(w,20000));
		for(int i=0;i<n;i++){
			G g;
			int m; cin >> m;
			for(int j=0;j<m;j++){
				int x, y; cin >> x >> y;
				g.push_back(P(x,y));
			}
			vg.push_back(g);
		}
		for(int i=0;i<n;i++){
			d1[i] = distCC(vd1,vg[i]);
			d2[i] = distCC(vd2,vg[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				dist[i][j] = distCC(vg[i],vg[j]);
				dist[j][i] = dist[i][j];
			}
		}
		for(int j=0;j<n;j++)
			for(int i=0;i<n;i++)
				for(int k=0;k<n;k++)
					dist[i][k] = min(dist[i][k], dist[i][j]+dist[j][k]);
		double ans = w;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				ans = min(ans, d1[i]+dist[i][j]+d2[j]);
			}
		}
		printf("%.8lf\n", ans);
	}
}