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 EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS )

class Point{
public:
  double x,y;
  Point(double x=0,double y=0):x(x),y(y){}
  Point operator + (Point p){return Point (x+p.x,y+p.y);}
  Point operator - (Point p){return Point (x-p.x,y-p.y);}
  Point operator * (double a){return Point (x*a,y*a);}
  Point operator / (double a){return Point (x/a,y/a);}
};

double norm( Point a ){ return a.x*a.x + a.y*a.y;}
double abs( Point a ){ return sqrt(norm(a)); }
double dot( Point a, Point b ){return a.x*b.x+a.y*b.y;}
double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}

class Segment{
public:
  Point p1,p2;
  Segment(Point a,Point b):p1(a),p2(b){}
};

int ccw(Point p0,Point p1,Point p2){
  Point a=p1-p0;
  Point b=p2-p0;
  if( cross(a, b) > EPS ) return 1;
  if( cross(a, b) < -EPS ) return -1;
  if( dot(a,b) < -EPS ) return 2;
  if( norm(a) < norm(b) ) return -2;
  return 0;
}

bool isIntersect(Point p1,Point p2,Point p3,Point p4){
  return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&
	  ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}

bool isIntersect(Segment s1,Segment s2){
  return isIntersect(s1.p1, s1.p2, s2.p1, s2.p2);
}

double getDistanceLP(Segment s,Point p){
  return abs(cross(s.p2-s.p1,p-s.p1)/abs(s.p2-s.p1));
}

double getDistanceSP(Segment s,Point p){
  if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
  if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
  return getDistanceLP(s,p);
}

double getDistance(Segment s1,Segment s2){
  if(isIntersect(s1,s2))return 0.0;
  return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
	     min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}

int W,N;
vector<Segment> t[300];
double d[300];
double cost[300][300];

double calc(int a,int b){
  double res=1e9;
  for(int i=0;i<(int)t[a].size();i++)
    for(int j=0;j<(int)t[b].size();j++)
      res=min(res,getDistance(t[a][i],t[b][j]));
  return res;
}

typedef pair<double,int> P;

void solve(){

  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++)
      cost[i][j]=calc(i,j);
    
  Segment si(Point(0,0),Point(0,1e8));
  Segment ti(Point(W,0),Point(W,1e8));

  priority_queue< P , vector<P> ,greater<P> > Q;

  for(int i=0;i<N;i++){
    d[i]=1e9;
    int size=t[i].size();
    for(int j=0;j<size;j++)
      d[i]=min(d[i],getDistance(t[i][j],si));
    Q.push(P(d[i],i));
  }

  while(!Q.empty()){
    P p=Q.top();Q.pop();
    if( p.first > EPS+d[p.second])continue;
    for(int to=0;to<N;to++){
      if( p.first+cost[p.second][to] < d[to]-EPS){
	d[to]=p.first+cost[p.second][to];
	Q.push(P(d[to],to));
      }
    }
  }

  double ans=W;
  for(int i=0;i<N;i++){
    double dist=1e9;
    int size=t[i].size();
    for(int j=0;j<size;j++){
      dist=min(dist,getDistance(t[i][j],ti));
    }
    ans=min(ans,d[i]+dist);
  }
  printf("%.8f\n",ans);
}

void init(){
  for(int i=0;i<300;i++){
    t[i].clear();
  }
}

int main(){
  while(1){
    cin>>W>>N;
    if(W==0&&N==0)break;
    init();
    for(int i=0;i<N;i++){
      int a;
      cin>>a;

      Point A,B,C;
      cin>>A.x>>A.y;
      C=A;
      for(int j=0;j<a-1;j++){
	B=A;
	cin>>A.x>>A.y;
	t[i].push_back((Segment){A,B});
      }
      t[i].push_back((Segment){C,A});
    }
    solve();
  }
  return 0;
}