#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-10;
const int MOD = 1000000007;

#include <complex>
#include <cmath>
#define PI 3.1415926535897932384 

// 実数の符号関数
inline int signum(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
//XY座標
#define X real()
#define Y imag()
// 点
typedef complex<double> P;
 

// 線分・半直線・直線
struct L { P pos, dir; L(P p=P(), P d=P()):pos(p),dir(d){}};


// std::norm はabs(p)*abs(p)なので遅い
inline double norm(P p){
	return p.X*p.X+p.Y*p.Y;
}

// 二つのベクトルの内積を計算する
inline double inp(const P& a, const P& b) {
	return (conj(a)*b).X;
}
// ベクトルpをベクトルbに射影したベクトルを計算する
inline P proj(const P& p, const P& b) {
	return b*inp(p,b)/norm(b);
}

// 点pから直線lに引いた垂線の足となる点を計算する
inline P perf(const L& l, const P& p) {
	L m(l.pos - p, l.dir);
	return (p + (m.pos - proj(m.pos, m.dir)));
}

double sp_distance(const L& s, const P& p) {
    const P r = perf(s, p);
    const double pos = ((r-s.pos)/s.dir).real();
    if (-EPS <= pos && pos <= 1 + EPS)
        return abs(r - p);
    return min(abs(s.pos - p),
               abs(s.pos+s.dir - p));
}
double ss_distance(const L& s, const L& t) {
    return min(min(sp_distance(s, t.pos),
            sp_distance(s, t.pos+t.dir)),
            min(sp_distance(t, s.pos),
            sp_distance(t, s.pos+s.dir)));
}

int n, m;
int w;
double g[300][300];
main(){
	ios::sync_with_stdio(false);
	while(cin >> w >> n, w){
		vector<vector<L>> ls(n+2);
		REP(i, n){
			cin >> m;
			P pp;
			REP(j, m){
				double x, y;
				cin >> x >> y;
				P p(x, y);
				if(j) ls[i].emplace_back(pp, p-pp);
				pp = p;
			}
			ls[i].emplace_back(pp, ls[i][0].pos - pp);
		}
		ls[n].emplace_back(P(0, 0), P(0, 20000));
		ls[n+1].emplace_back(P(w, 0), P(0, 20000));
		REP(i, n+2)REP(j, i){
			double d = 1e7;
			REP(k, ls[i].size())REP(l, ls[j].size()) 
				d = min(d, ss_distance(ls[i][k], ls[j][l]));
			g[i][j] = g[j][i] = d;
		}
		REP(k, n+2)REP(i, n+2)REP(j, n+2) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
		
		printf("%.10f\n", g[n][n+1]);
	}
	return 0;
}

Measured memory_bytes: 1808