#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(auto i = (c).begin(); i != (c).end(); ++i)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<int, int> PI;

const ll inf = 1e15;
const ll mod = 1000 * 1000 * 1000 + 7;
const double eps = 1e-9;

template <typename S, typename T> ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()){
    if (i > 0) out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

typedef complex<double> P;
typedef vector<P> Poly;
typedef pair<P, P> L;

double cross(const P &a, const P &b) { return imag(conj(a)*b); }
double dot(const P &a, const P &b) { return real(conj(a)*b); }
int ccw(P a, P b, P c) {
	double d = cross(b -= a, c -= a);
	return d>eps ? 1 : d<-eps ? -1 : dot(b,c)<-eps ? 2 : norm(b)+eps<norm(c) ? -2 : 0;
}

#define DI(l) ((l).second-(l).first)
#define CCW(l, a) ccw((l).first, (l).second, (a))

bool intersectSS(const L &s, const L &t) { return CCW(s,t.first)*CCW(s,t.second)<=0 && CCW(t,s.first)*CCW(t,s.second)<=0; }

double distanceSP(const L &l, const P &p) {
	P d = DI(l);
	if (dot(p-l.second, d) >= 0) return abs(l.second-p);
	if (dot(p-l.first, d) <= 0) return abs(l.first-p);
	return abs(cross(d, p-l.first) / abs(d));
}

double distanceSS(const L &s, const L &t) {
	if (intersectSS(s, t)) return 0;
	return min(min(distanceSP(s, t.first), distanceSP(s, t.second)), min(distanceSP(t, s.first), distanceSP(t, s.second)));
}

double distance(const Poly &p, const Poly &q){
  double res = inf;
  REP(i, p.size()) REP(j, q.size()){
    L lp(p[i], p[(i + 1) % p.size()]);
    L lq(q[j], q[(j + 1) % q.size()]);
    res = min(res, distanceSS(lp, lq));
    res = min(res, distanceSP(lp, q[j]));
    res = min(res, distanceSP(lq, p[i]));
  }
  return res;
}

int main(){
  int W, N;
  while (cin >> W >> N && W){
    vector<Poly> ps(N);

    REP(i, N){
      int M;
      double x, y;
      cin >> M;
      REP(j, M){
        cin >> x >> y;
        ps[i].emplace_back(x, y);
      }
    }
    
    vector<vector<double> > D(N + 2, vector<double>(N + 2, 0.0));
    REP(i, N) REP(j, N) if (i != j){
      D[i][j] = distance(ps[i], ps[j]);
    }
    REP(i, N) {
      D[i][N] = D[i][N + 1] = 1e9;
      REP(j, ps[i].size()){
        D[i][N] = min(D[i][N], ps[i][j].real());
        D[i][N + 1] = min(D[i][N + 1], W - ps[i][j].real());
      }
      D[N][i] = D[i][N];
      D[N + 1][i] = D[N + 1][i];
    }
    D[N][N + 1] = D[N + 1][N] = W;

    REP(k, D.size()) REP(i, D.size()) REP(j, D.size()) {
      D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
    }
    cout << fixed << setprecision(20) << D[N][N + 1] << endl;
  }
  return 0;
}