#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<int(n);++i)
using namespace std;

template<class T>bool chmin(T &a, const T &b) {return (b<a)?(a=b,1):0;}

using R=long double;
const R EPS = 1E-8; 
inline int sgn(const R& r){ return (r > EPS) - (r < -EPS);}

using P=complex<R>;
using L=struct{P s,t;};

inline R dot(P o,P a,P b){a-=o,b-=o; return real(conj(a)*b);}
inline R det(P o,P a,P b){a-=o,b-=o; return imag(conj(a)*b);}

R dsp(L s,P p){
	if(sgn(dot(s.s,s.t,p))<=0) return abs(p-s.s);
	if(sgn(dot(s.t,s.s,p))<=0) return abs(p-s.t);
	return abs(det(s.s,s.t,p))/abs(s.t-s.s);
}

R dss(L a,L b){return min({dsp(a,b.s),dsp(a,b.t),dsp(b,a.s),dsp(b,a.t)});}

const R inf=1e60;

int main(void){
	int w,m;
	cout.precision(20);
	while(cin >> w >> m,w){
		vector<L> pol[210];

		rep(i,m){
			int n,x[55],y[55];
			cin >> n;
			rep(j,n) cin >> x[j] >> y[j];
			rep(j,n) pol[i].push_back({P(x[j],y[j]),P(x[(j+1)%n],y[(j+1)%n])});
		}

		const int s=m,t=m+1,n=m+2;
		pol[s].push_back({P(0,-1e5),P(0,1e5)});
		pol[t].push_back({P(w,-1e5),P(w,1e5)});
		
		R dist[210][210];
		rep(i,n) dist[i][i]=0.0;

		rep(j,n)rep(i,j){
			R d=inf;
			for(auto &il:pol[i])
				for(auto &jl:pol[j]) 
					chmin(d,dss(il,jl));
			dist[i][j]=dist[j][i]=d;
		}
	
		rep(k,n)rep(i,n)rep(j,n) chmin(dist[i][j],dist[i][k]+dist[k][j]);
		cout << fixed << dist[s][t] << endl;
 	}
	return 0;
}