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 <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <complex>

using namespace std;
typedef  long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


#define EPS 1e-12
#define rep(i,n) for(int i=0;i<(int)(n);++i)

typedef complex<double> C;

const double PI = 4*atan(1.0);

namespace std
{
    bool operator < (const C a, const C b) {
        return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
    }
}

struct L : public vector<C>
{
    L(){}
    L(const C a, const C b) {
        push_back(a); push_back(b);
    }
};

bool eq(double a,double b)
{
  return (-EPS<a-b&&a-b<EPS);
}

bool eq(C c1,C c2)
{
  return (eq(c1.real(),c2.real()) && eq(c1.imag(),c2.imag()));
}

//?????¶??????sqrt
double Sqrt(double x)
{
    if(x<0) return 0;
    else    return sqrt(x);
}

//??£??????
C normalize(C c)
{
    return c / abs(c);
}

//?§????(rad)
double getarg(C a,C b){
    return arg(b*conj(a));
}

//??????
double cross(const C a, const C b)
{
    return imag(conj(a)*b);
}
//??????
double dot(const C a, const C b)
{
    return real(conj(a)*b);
}

int ccw(C a, C b, C c)
{
    b -= a; c -= a;
    if(cross(b, c) > 0)   return +1;       // counter clockwise
    if(cross(b, c) < 0)   return -1;       // clockwise
    if(dot(b, c) < 0)     return +2;       // c--a--b on line
    if(norm(b) < norm(c)) return -2;       // a--b--c on line
    return 0;   //b--a--c on line
}
//??´???????????????????????????(????????´??????True)
bool intersectLL(const L& l, const L& m)
{
    return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;
}
//??´?????¨?????????????????????(????????±??????????????¨??????)
bool intersectLS(const L& l, const L& s)
{
    return cross(l[1]-l[0], s[0]-l[0]) * cross(l[1]-l[0], s[1]-l[0]) < EPS;
}
//??´?????¨????????????(??±???)??????
bool intersectLP(const L& l, const C p)
{
    return abs(cross(l[1]-p, l[0]-p)) < EPS;
}
//??????????????????????????????(????????±??????????????¨??????)
bool intersectSS(const L& s, const L& t)
{
    return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
//????????¨????????????(??±???)??????
bool intersectSP(const L& s, const C p)
{
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;
}
//??´???????????????????????????
C crosspointLL(const L& l, const L& m)
{
    double A = cross(l[1] - l[0], m[1] - m[0]);
    double B = cross(l[1] - l[0], l[1] - m[0]);
    //????????´????????¨???
    if(abs(A) < EPS && abs(B) < EPS){
        return m[0];
    }
    return m[0] + B / A * (m[1] - m[0]);
}
//???p?????´???l???????°???±
C projection(const L& l, const C p)
{
    double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
    return l[0] + t*(l[0]-l[1]);
}


//?????¨??´???????????¢
double distanceLP(const L &l, const C p)
{
    return abs(p - projection(l, p));
}
//??´?????¨??´???????????¢
double distanceLL(const L &l, const L &m)
{
    return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
//??´?????¨??????????????¢
double distanceLS(const L &l, const L &s)
{
    if (intersectLS(l, s)) return 0;
    return min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}
//????????¨???????????¢
double distanceSP(const L &s, const C p)
{
    const C r = projection(s, p);
    if (intersectSP(s, r)) return abs(r - p);
    return min(abs(s[0] - p), abs(s[1] - p));
}
//????????¨??????????????¢
double distanceSS(const L &s, const L &t)
{
    if (intersectSS(s, t)) return 0;
    return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}


int main(){
	double w;
	int n;
	int m;
	while(cin >> w >> n&&w!=0){
		vector<vector<C> > p(n+2,vector<C>());
		p[0].push_back(C(0.0,-100000.0));
		p[0].push_back(C(0.0,100000.0));
		p[n+1].push_back(C(w,-100000.0));
		p[n+1].push_back(C(w,100000.0));
		rep(i,n){
			cin >> m;
			rep(j,m){
				double a,b;
				cin >> a >> b;
				p[i+1].push_back(C(a,b));
			}
		}
		vector<vector<double> > dst(n+2,vector<double>(n+2,100000000.0));
		for(int i=0;i<n+2;i++){
			for(int j=i+1;j<n+2;j++){
				for(int x=0;x<p[i].size();x++){
					L l1;
					if(x!=p[i].size()-1)l1=L(p[i][x],p[i][x+1]);
					else l1=L(p[i][x],p[i][0]);
					
					for(int y=0;y<p[j].size();y++){
						double d = distanceSP(l1,p[j][y]);		
						dst[i][j] = min(dst[i][j],d);
						dst[j][i] = dst[i][j];
					}
					for(int y=0;y<p[j].size();y++){
						L l2;
						if(y!=p[j].size()-1) l2=L(p[j][y],p[j][y+1]);
						else l2=L(p[j][y],p[j][0]);					
						double d = distanceSP(l2,p[i][x]);		
						dst[i][j] = min(dst[i][j],d);
						dst[j][i] = dst[i][j];
					}
				}
			}
		}
		double pinf = 1000000000;
		vector<double> d(n+2,pinf);
		d[0] = 0;
		priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > > pq;
		pq.push(MP(0,0));
		while(!pq.empty()){
			pair<double,int> s;
			s = pq.top();
			pq.pop();
			if(d[s.second]< s.first)continue;
			for(int i=0;i<n+2;i++){
				if(d[i] > d[s.second]+dst[s.second][i]){
					d[i] = d[s.second]+dst[s.second][i];
					pq.push(MP(d[i],i));
				}
			}
			
		}
		cout.precision(10);
		cout << d[n+1] << endl;
	}
	return 0;
}