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++)
#define fs first
#define sc second
#define pb push_back
#define sz size()
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P,P> L;
typedef pair<D,int> pdi;
 
const D EPS = 1e-8;
 
inline D dot(P x, P y){return real(conj(x)*y);}
inline D cross(P x, P y){return imag(conj(x)*y);}
 
inline D seg_p_dis(L a,P x){
  if(dot(a.sc-a.fs,x-a.fs)<EPS)return abs(x-a.fs);
  if(dot(a.fs-a.sc,x-a.sc)<EPS)return abs(x-a.sc);
  return abs(cross(a.sc-a.fs,x-a.fs))/abs(a.sc-a.fs);
}
 
int main(){
  int w,n,m,x,y;
  while(scanf("%d%d",&w,&n)){
    if(w==0)return 0;
 
    vector< vector<L> > l(n);
    rep(i,n){
      scanf("%d",&m);
      vector<P> p(m);
      rep(j,m){
	scanf("%d%d",&x,&y);
	p[j] = P(x,y);
      }
      rep(j,m)l[i].push_back(L(p[j],p[(j+1)%m]));
    }
 
    int v = n+2;
    vector< vector<pdi> > g(v);
    g[v-2].pb(pdi(w,v-1));
    L ls = L(P(0,0),P(0,1e4)), lt = L(P(w,0),P(w,1e4));
    rep(i,n){
      D dis = w;
      for(L a : l[i])dis = min(dis, seg_p_dis(ls,a.fs));
      g[v-2].pb(pdi(dis, i));
      dis = w;
      for(L a : l[i])dis = min(dis, seg_p_dis(lt,a.fs));
      g[i].pb(pdi(dis,v-1));
      rep(j,i){
	dis = w;
	for(L a : l[i])for(L b : l[j]){
	  dis = min(dis, seg_p_dis(a,b.fs));
	  dis = min(dis, seg_p_dis(b,a.fs));
	}
	g[i].pb(pdi(dis,j)); g[j].pb(pdi(dis,i));
      }
    }

    vector<D> d(v,1e9);
    d[v-2] = 0;
    bool use[220];
    memset(use,0,sizeof(use));
 
    for(;;){
      int u = -1;
      rep(i,v){
	if(!use[i] && (u<0 || d[u] > d[i]+EPS) )u = i;
      }
      if(u<0 || u==v-1)break;
      use[u] = true;
       
      for(pdi nxt : g[u]){
	D ncost = d[u] + nxt.fs;
	if(!use[nxt.sc] && d[nxt.sc] > ncost +EPS)d[nxt.sc] = ncost;
      }
    }
    printf("%.9lf\n",d[v-1]);
  }
}