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 eq(a, b) (fabs((a)-(b)) < EPS)
#define lt(a, b) ((a)-(b) < -EPS)
#define le(a, b) (eq(a, b) || lt(a, b))

struct Point {
  double x, y;
  Point(double x = 0.0, double y = 0.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 k) { return Point(x*k, y*k); }
  Point operator / (double k) { return Point(x/k, y/k); }
  bool operator < (const Point& p) {
    return x != p.x ? x < p.x : y < p.y;
  }
  bool operator == (const Point& p) {
    return eq(x, p.x) && eq(y, p.y);
  }  
};
using Vector = Point;
double norm(Vector v) { return v.x*v.x+v.y*v.y; }
double abs(Vector v) { return sqrt(norm(v)); }
double dot(Vector a, Vector b) { return a.x*b.x+a.y*b.y; }
double cross(Vector a, Vector b) { return a.x*b.y-a.y*b.x; }
#define CCW 1
#define CW -1
#define ON_BACK 2
#define ON_FRONT -2
#define ON_SEG 0
int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1-p0;
  Vector b = p2-p0;
  if(cross(a, b) > EPS) return CCW;
  if(cross(a, b) < -EPS) return CW;
  if(dot(a, b) < -EPS) return ON_BACK;
  if(norm(a) < norm(b)) return ON_FRONT;
  return ON_SEG;
}
bool intersect(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);
}
double getDistance(Point a, Point b) {
  return abs(a-b);
}
struct Segment {
  Point s, t;
  Segment(Point s = Point(), Point t = Point()):s(s), t(t){}
};
using Line = Segment;
bool intersect(Segment s1, Segment s2) {
  return intersect(s1.s, s1.t, s2.s, s2.t);
}
double getDistanceLP(Line l, Point p) {
  return abs(cross(l.t-l.s, p-l.s)/abs(l.t-l.s));
}
double getDistanceSP(Segment s, Point p) {
  if(dot(s.t-s.s, p-s.s) < 0.0) return abs(p-s.s);
  if(dot(s.s-s.t, p-s.t) < 0.0) return abs(p-s.t);
  return getDistanceLP(s, p);
}
double getDistance(Segment s1, Segment s2) {
  if(intersect(s1, s2)) return 0.0;
  return min(min(getDistanceSP(s1, s2.s), getDistanceSP(s1, s2.t)),
	     min(getDistanceSP(s2, s1.s), getDistanceSP(s2, s1.t)));
}
using Polygon = vector<Point>;
double getDistance(Polygon g, Polygon h) {
  int n = g.size(), m = h.size();
  double res = 1e9;
  vector<Segment> ss, tt;
  for(int i = 0; i < n; i++) {
    ss.push_back(Segment(g[i], g[(i+1)%n]));
  }
  for(int i = 0; i < m; i++) {
    tt.push_back(Segment(h[i], h[(i+1)%m]));    
  }
  for(Segment s : ss) {
    for(Segment t : tt) {
      res = min(res, getDistance(s, t));
    }
  }
  return res;
}

int W, N;
vector<Polygon> poly;

int main() {
  while(cin >> W >> N, W || N) {
    poly.clear();
    poly.resize(N);
    for(int i = 0; i < N; i++) {
      int M; cin >> M;
      for(int j = 0; j < M; j++) {
	double x, y;
	cin >> x >> y;
	poly[i].push_back(Point(x, y));
      }
    }
    Polygon p, q;
    p.push_back(Point(0, 0));
    p.push_back(Point(0, 10000));
    p.push_back(Point(-1, 10000));
    p.push_back(Point(-1, 0));
    q.push_back(Point(W, 0));
    q.push_back(Point(W, 10000));
    q.push_back(Point(W+1, 10000));
    q.push_back(Point(W+1, 0));
    poly.push_back(p);
    poly.push_back(q);
    double dist[300][300];
    int n = poly.size();
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
	dist[i][j] = getDistance(poly[i], poly[j]);
      }    
    }
    for(int k = 0; k < n; k++)
      for(int i = 0; i < n; i++)
	for(int j = 0; j < n; j++)
	  dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
    printf("%.12f\n", dist[N][N+1]);
  }
  return 0;
}