#include<bits/stdc++.h>

using namespace std;

const double EPS = 1e-8, PI = acos(-1);

inline bool eq(double a, double b) { return fabs(b - a) < EPS; }

struct Point
{
  double x, y;

  Point() {}

  Point(double x, double y) : x(x), y(y) {}

  Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); }

  Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }

  Point operator*(const double b) const { return Point(x * b, y * b); }

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

  Point operator/(const double b) const { return Point(x / b, y / b); }

  bool operator<(const Point &b) const { return x != b.x ? x < b.x : y < b.y; }

  bool operator==(const Point &b) const { return eq(x, b.x) && eq(y, b.y); }

  double norm() { return x * x + y * y; }

  double arg() { return atan2(x, y); }

  double abs() { return sqrt(norm()); }

  Point rotate(double theta) { return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y); }

  Point rotate90() { return Point(-y, x); }

  friend ostream &operator<<(ostream &os, Point &p) { return os << "(" << p.x << "," << p.y << ")"; }

  friend istream &operator>>(istream &is, Point &a) { return is >> a.x >> a.y; }
};

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

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

struct Line
{
  Point a, b;

  Line() {};

  Line(Point a, Point b) : a(a), b(b) {};

  friend ostream &operator<<(ostream &os, Line &p) { return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << "," << p.b.y << ")"; }

  friend istream &operator>>(istream &is, Line &a) { return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y; }
};

struct Segment
{
  Point a, b;

  Segment() {};

  Segment(Point a, Point b) : a(a), b(b) {};

  friend ostream &operator<<(ostream &os, Segment &p) { return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << "," << p.b.y << ")"; }

  friend istream &operator>>(istream &is, Segment &a) { return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y; }
};

int ccw(const Point &a, Point b, Point c)
{
  b = b - a, c = c - a;
  if(cross(b, c) > EPS) return +1;
  if(cross(b, c) < -EPS) return -1;
  if(dot(b, c) < 0) return +2;
  if(b.norm() < c.norm()) return -2;
  return 0;
}

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

bool Intersect(const Segment &s, const Point &p)
{
  return ccw(s.a, s.b, p) == 0;
}

Point Projection(const Segment &l, const Point &p)
{
  double t = dot(p - l.a, l.a - l.b) / (l.a - l.b).norm();
  return l.a + (l.a - l.b) * t;
}

double Distance(const Segment &s, const Point &p)
{
  Point r = Projection(s, p);
  if(Intersect(s, r)) return (r - p).abs();
  return min((s.a - p).abs(), (s.b - p).abs());
}

double Distance(const Segment &a, const Segment &b)
{
  if(Intersect(a, b)) return 0;
  return min(min(Distance(a, b.a), Distance(a, b.b)), min(Distance(b, a.a), Distance(b, a.b)));
}

struct Circle
{
  Point p;
  double r;

  Circle() {};

  Circle(Point p, double r) : p(p), r(r) {};
};

typedef vector< Segment > Segments;

template< typename T = int >
void Warshall(vector< vector< T > > &g)
{
  for(int k = 0; k < g.size(); k++) {
    for(int i = 0; i < g.size(); i++) {
      for(int j = 0; j < g.size(); j++) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
}

int main()
{
  int W, M;
  while(cin >> W >> M, W) {
    vector< Segments > polys(M + 2);
    for(int i = 0; i < M; i++) {
      int sz;
      cin >> sz;
      vector< Point > pts(sz);
      for(auto &p : pts) cin >> p;
      for(int j = 0; j < sz; j++) {
        polys[i].emplace_back(pts[j], pts[(j + 1) % sz]);
      }
    }
    int s = M, t = M + 1;
    polys[s].emplace_back(Point(0, -1e9), Point(0, 1e9));
    polys[t].emplace_back(Point(W, -1e9), Point(W, 1e9));
    M += 2;

    vector< vector< double > > mat(M, vector< double >(M, 1e9));
    for(int i = 0; i < M; i++) {
      mat[i][i] = 0.0;
      for(int j = 0; j < i; j++) {
        double dist = 1e9;
        for(auto &s1 : polys[i]) {
          for(auto &s2 : polys[j]) {
            dist = min(dist, Distance(s1, s2));
          }
        }
        mat[i][j] = mat[j][i] = dist;
      }
    }
    Warshall< double >(mat);
    cout << fixed << setprecision(15) << mat[s][t] << endl;
  }

}

Measured memory_bytes: 3320