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>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define X real()
#define Y imag()
D dot(P a,P b) {return (conj(a)*b).X; }
D cross(P a,P b) {return(conj(a)*b).Y;}
int ccw(P a,P b,P c){
  b-=a;c-=a;
  if(cross(b,c) >EPS)return +1;
  if(cross(b,c)< -EPS)return -1;
  if(dot(b,c)< -EPS)return +2;
  if(norm(b)<norm(c))return -2;
  return 0;
}
bool isecSS(P a1, P a2, P b1, P b2) {
  return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;
}
bool isecSP(P a1,P a2,P b) {
  return abs(a1 - b) + abs(a2 - b) - abs(a2 - a1) < EPS;
}
P proj(P a1,P a2,P p){return a1+dot(a2-a1, p-a1)/norm(a2-a1)*(a2-a1);}
D distSP(P a1,P a2,P p) {
  P r=proj(a1,a2,p);
  if(isecSP(a1,a2,r))return abs(r-p);
  return min(abs(a1-p),abs(a2-p));
}
 
D distSS(P a1,P a2,P b1,P b2) {
  if (isecSS(a1,a2,b1,b2))return 0;
  return min(min(distSP(a1,a2,b1),distSP(a1,a2,b2)),min(distSP(b1,b2,a1),distSP(b1,b2,a2)));
}
int n,x,x1,y2;
double h,a[222][222];
vector<P>v[222];
int main(){
  while(cin>>h>>n,h){
    r(i,222)r(j,222)a[i][j]=1e15;
    r(i,222)v[i].clear();
    v[n].push_back(P(0,0));
    v[n].push_back(P(0,1e9));
    v[n+1].push_back(P(h,0));
    v[n+1].push_back(P(h,1e9));
    r(i,n){
      cin>>x;
      while(x--){
        cin>>x1>>y2;
        v[i].push_back(P(x1,y2));
      }
    }
    r(i,n+2)r(j,n+2)r(k,v[i].size())r(l,v[j].size())
      a[i][j]=min(a[i][j],distSS(v[i][k],v[i][(k+1)%(int)v[i].size()],v[j][l],v[j][(l+1)%(int)v[j].size()]));
    r(k,n+2)r(i,n+2)r(j,n+2)a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
    printf("%.9f\n",a[n][n+1]);
  }
}