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 <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }

constexpr double EPS = 1e-9;

struct point {
	double x, y;
	explicit point(double x_ = 0.0, double y_ = 0.0):x(x_), y(y_) {}
	point(const point &p):x(p.x), y(p.y) {}

	point operator+(const point &p) const {
		return point(x + p.x, y + p.y);
	}

	point operator-(const point &p) const {
		return point(x - p.x, y - p.y);
	}

	point operator*(double s) const {
		return point(x * s, y * s);
	}

	point operator*(const point &p) const {
		return point(x * p.x - y * p.y, x * p.y + y * p.x);
	}

	point operator/(double s) const {
		return point(x / s, y / s);
	}

	bool operator<(const point &p) const {
		return x + EPS < p.x || (abs(x - p.x) < EPS && y + EPS < p.y);
	}

	bool operator==(const point &p) const {
		return abs(x - p.x) < EPS && abs(y - p.y) < EPS;
	}
};

double norm(const point &p) {
	return p.x * p.x + p.y * p.y;
}

double abs(const point &p) {
	return sqrt(norm(p));
}

double dot(const point &a, const point &b) {
	return a.x * b.x + a.y * b.y;
}

double cross(const point &a, const point &b) {
	return a.x * b.y - a.y * b.x;
}

struct line {
	point a, b;
	line(const point &a_, const point &b_):a(a_), b(b_){}
};

struct segment {
	point a, b;
	segment(const point &a_, const point &b_):a(a_), b(b_){}
};

typedef vector<point> polygon;

int ccw(const point &a, point b, point c) {
	b = b - a;
	c = c - a;
	const double tmp = cross(b, c);
	if(tmp > EPS) return 1; // ccw
	if(tmp < -EPS) return -1; // cw
	if(dot(b, c) < 0) return 2; // c, a, b 順に一直線上
	if(norm(b) < norm(c)) return -2; // a, b, c 順に一直線上
	return 0; //a, c, b 順で一直線上
}

bool intersect(const segment &s, const segment &t) {
	return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0
		&& ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

double dist(const line &l, const point &p) {
	return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);
}

double dist(const segment &s, const point &p) {
	if(dot(s.b - s.a, p - s.a) < 0) return abs(p - s.a);
	if(dot(s.a - s.b, p - s.b) < 0) return abs(p - s.b);
	return dist(line(s.a, s.b), p);
}

double dist(const segment &s, const segment &t) {
	if(intersect(s, t)) return 0;
	return min(min(dist(s, t.a), dist(s, t.b)), min(dist(t, s.a), dist(t, s.b)));
}

double dist(const polygon &P1, const polygon &P2) {
	double res = INT_MAX;

	point p1 = P1.back();
	for(const auto &c1 : P1) {
		const segment s1(c1, p1);

		point p2 = P2.back();

		for(const auto &c2 : P2) {
			const segment s2(c2, p2);
			chmin(res, dist(s1, s2));
			p2 = c2;
		}

		p1 = c1;
	}

	return res;
}

double dijkstra(int s, int g, const vector<vector<double>> &mat) {
	const int n = mat.size();

	vector<double> dist(n, INT_MAX);
	vector<bool> used(n, false);

	dist[s] = 0.0;

	for(int iter = 0; iter < n; ++iter) {
		int v = -1;

		for(int u = 0; u < n; ++u) {
			if(!used[u] && (v == -1 || dist[v] > dist[u] + EPS)) {
				v = u;
			}
		}

		if(v == g) return dist[g];

		used[v] = true;

		for(int to = 0; to < n; ++to) {
			chmin(dist[to], dist[v] + mat[v][to]);
		}
	}
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	constexpr int MAX_Y = 10000;

	for(int w, n; cin >> w >> n && w;) {
		const int V = n + 2;

		vector<polygon> pillars;
		pillars.reserve(V);
		pillars.emplace_back(polygon{point(-1, 0), point(-1, MAX_Y), point(0, MAX_Y), point(0, 0)});
		pillars.emplace_back(polygon{point(w, 0), point(w, MAX_Y), point(w + 1, MAX_Y), point(w + 1, 0)});

		for(int i = 0; i < n; ++i) {
			int m;
			cin >> m;

			polygon P;
			P.reserve(m);

			for(int j = 0; j < m; ++j) {
				int x, y;
				cin >> x >> y;
				P.emplace_back(x, y);
			}

			pillars.emplace_back(move(P));
		}

		vector<vector<double>> mat(V, vector<double>(V));

		for(int v = 0; v < V; ++v) {
			mat[v][v] = 0.0;

			for(int u = v + 1; u < V; ++u) {
				mat[v][u] = mat[u][v] = dist(pillars[v], pillars[u]);
			}
		}

		cout << dijkstra(0, 1, mat) << endl;
	}

	return EXIT_SUCCESS;
}