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<algorithm>
#include<cmath>
#include<complex>

using namespace std;

typedef complex<double> P;

double cross(P a,P b){
  return a.real()*b.imag()-a.imag()*b.real();
}

double dot(P a,P b){
  return a.real()*b.real()+a.imag()*b.imag();
}

double dist(P a,P b,P c){
  if(dot(c-b,a-b)*dot(c-a,b-a)>0){
    return fabs(cross(a-b,c-b)/abs(a-b));
  }else{
    return min(abs(c-b),abs(c-a));
  }
}

int main(){
  for(int W,N;cin>>W>>N,W|N;){
    vector<int> x[223],y[223];
    double d[223];
    for(int i=0;i<N;i++){
      d[i]=1e9;
      int M;
      cin>>M;
      x[i].resize(M);
      y[i].resize(M);
      for(int j=0;j<M;j++){
	cin>>x[i][j]>>y[i][j];
	d[i]=min<double>(d[i],x[i][j]);
      }
    }
    double ans=W;
    bool u[223]={};
    for(int i=0;i<N;i++){
      double m=1e9;
      int mx;
      for(int j=0;j<N;j++){
	if(!u[j]&&d[j]<m){
	  m=d[j];
	  mx=j;
	}
      }
      u[mx]=true;
      for(auto e:x[mx]){
	ans=min(ans,d[mx]+W-e);
      }
      for(int j=0;j<x[mx].size();j++){
	for(int k=0;k<N;k++){
	  for(int l=0;l<x[k].size();l++){
	    d[k]=min(d[k],d[mx]+hypot(x[mx][j]-x[k][l],y[mx][j]-y[k][l]));
	    auto t=d[k];
	    d[k]=min(d[k],d[mx]+dist(P(x[mx][j],y[mx][j]),P(x[mx][(j+1)%x[mx].size()],y[mx][(j+1)%x[mx].size()]),P(x[k][l],y[k][l])));
	    d[k]=min(d[k],d[mx]+dist(P(x[k][l],y[k][l]),P(x[k][(l+1)%x[k].size()],y[k][(l+1)%y[k].size()]),P(x[mx][j],y[mx][j])));
	  }
	}
      }
    }
    cout.precision(9);
    cout<<fixed<<ans<<endl;
  }
}