#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;
}