1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "bits/stdc++.h"

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;
using ld = long double;

constexpr int nax = 50 * 1000 + 105;
constexpr int infty = 1000 * 1000 * 1000 + 5;
constexpr int mod = 1000 * 1000 * 1000 + 7;

time_t poczatek;

bool MamSekunde() {
  return time(NULL) - poczatek <= 13;
  //return time(NULL) - poczatek <= 4;
}

template <typename T>
void Unique(vector<T>& v) {
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
}

ll PointPointDistance(const pair<ll, ll>& a, const pair<ll, ll>& b) {
  return max(abs(a.first - b.first), abs(a.second - b.second));
}

ll PointSegmentDistance(const pair<ll, ll>& p, pair<ll, ll> a, pair<ll, ll> b) {
  if (a.first == b.first) {
    if (a.second > b.second) swap(a, b);
    if (p.second < a.second) return PointPointDistance(p, a);
    if (p.second <= b.second) return abs(a.first - p.first);
    return PointPointDistance(p, b);
  } else {
    assert(a.second == b.second);
    if (a.first > b.first) swap(a, b);
    if (p.first < a.first) return PointPointDistance(p, a);
    if (p.first <= b.first) return abs(a.second - p.second);
    return PointPointDistance(p, b);
  }
}

ll SegmentSegmentDistance(pair<ll, ll> a1, pair<ll, ll> b1,
                          pair<ll, ll> a2, pair<ll, ll> b2) {
  if ((a1.first == b1.first) != (a2.first == b2.first)) {
    return min(min(PointSegmentDistance(a1, a2, b2),
                   PointSegmentDistance(b1, a2, b2)),
               min(PointSegmentDistance(a2, a1, b1),
                   PointSegmentDistance(b2, a1, b1)));
  }
  if (a1.first == b1.first) {
    assert(a2.first == b2.first);
    if (a1.second > b1.second) swap(a1, b1);
    if (a2.second > b2.second) swap(a2, b2);
    if (b1.second < a2.second) return PointPointDistance(b1, a2);
    if (b2.second < a1.second) return PointPointDistance(b2, a1);
    return abs(a1.first - a2.first);
  } else {
    assert(a1.second == b1.second);
    assert(a2.second == b2.second);
    if (a1.first > b1.first) swap(a1, b1);
    if (a2.first > b2.first) swap(a2, b2);
    if (b1.first < a2.first) return PointPointDistance(b1, a2);
    if (b2.first < a1.first) return PointPointDistance(b2, a1);
    return abs(a1.second - a2.second);
  }
}

struct Poziomica {
  vector<pair<ll, ll>> punkty;
  int n;
  ll pole;
  ll distance;

  inline int Next(int i) const {
    if (i == n - 1) return 0;
    return i + 1;
  }

  void Init() {
    n = (int) punkty.size();
    pole = 0;
    for (int i = 0; i < n; i++) {
      const int j = Next(i);
      if (punkty[i].second == punkty[j].second) {
        pole += (punkty[i].first - punkty[j].first) * punkty[i].second;
      }
    }
    assert(pole > 0);
  }

  bool Zawiera(ll x, ll y) const {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      const int j = Next(i);
      if (punkty[i].second == punkty[j].second and punkty[i].second > y) {
        int a = punkty[i].first, b = punkty[j].first;
        if (a > b) swap(a, b);
        if (a < x and x <= b) {
          cnt ^= 1;
        }
      }
    }
    return cnt;
  }

  ll Distance(const Poziomica& p) const {
    ll result = numeric_limits<ll>::max();
    auto Add = [&result](ll d) -> void {
      result = min(result, d);
    };
    for (int i = 0; i < n; i++) {
      const int j = Next(i);
      for (int pi = 0; pi < p.n; pi++) {
        const int pj = p.Next(pi);
        Add(PointPointDistance(punkty[i], p.punkty[pi]));
        Add(PointSegmentDistance(punkty[i], p.punkty[pi], p.punkty[pj]));
        Add(PointSegmentDistance(p.punkty[pi], punkty[i], punkty[j]));
        Add(SegmentSegmentDistance(punkty[i], punkty[j],
                                   p.punkty[pi], p.punkty[pj]));
      }
    }
    return result - 2 * (!Zawiera(p.punkty[0].first, p.punkty[0].second));
  }
};

class Graf {
 public:
  Graf(int n) {
    this->n = n;
    graf.resize(n);
    odl.resize(n, numeric_limits<ll>::max());
  }

  void AddEdge(int a, int b, ll c) {
    assert(0 <= a and a < n);
    assert(0 <= b and b < n);
    graf[a].emplace_back(b, c);
    graf[b].emplace_back(a, c);
  }

  void Dijkstra(int w) {
    vector<bool> done(n, false);
    odl[w] = 0;
    for (int i = 0; i < n; i++) {
      w = -1;
      ll best_odl = numeric_limits<ll>::max();
      for (int j = 0; j < n; j++) {
        if (done[j]) continue;
        if (odl[j] < best_odl) {
          w = j;
          best_odl = odl[j];
        }
      }
      assert(w != -1);
      done[w] = true;
      for (const auto& g : graf[w]) {
        const int neighbour = g.first;
        const ll dist = odl[w] + g.second;
        if (odl[neighbour] > dist) {
          odl[neighbour] = dist;
        }
      }
    }
  }

  ll Odl(int w) const {
    return odl[w];
  }

 private:
  int n;
  vector<ll> odl;
  vector<vector<pair<int, ll>>> graf;
};

int N;
vector<Poziomica> poziomice;
ll ultra_wynik = 1;

void DodajDoUltraWyniku(ll x, ll y, ll wynik);
ll LiczDist(ll x, ll y);

void Wzarzaj(ll min_x, ll max_x, ll min_y, ll max_y) {
  ll x = min_x + (rand() % ((max_x - min_x) / 2 + 1)) * 2;
  ll y = min_y + (rand() % ((max_y - min_y) / 2 + 1)) * 2;
  ll d = LiczDist(x, y);
  DodajDoUltraWyniku(x, y, d);
  ll pop_skok = -1;
  for (ll skok = 1000ll; skok > 0; skok *= 0.95) {
    if (skok == pop_skok) {
      break;
    }
    constexpr int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    constexpr int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    for (int j = 0; j < 8; j++) {
      const ll nx = x + dx[j] * 2;
      const ll ny = y + dy[j] * 2;
      const ll nd = LiczDist(nx, ny);
      if (nd > d) {
        x += dx[j] * 2 * skok;
        y += dy[j] * 2 * skok;
        break;
      }
    }
    d = LiczDist(x, y);
    DodajDoUltraWyniku(x, y, d);
    pop_skok = skok;
  }
}

void DodajDoUltraWyniku(ll x, ll y, ll wynik) {
  if (wynik <= ultra_wynik) return;
  if (poziomice[0].Zawiera(x, y)) {
    debug() << "DodajDoUltraWyniku(" imie(x) imie(y) imie(wynik) ")";
    ultra_wynik = wynik;
  }
}

ll LiczDist(ll x, ll y) {
  //debug() << "LiczDist(" imie(x) imie(y) ")";
  assert(x & 1);
  assert(y & 1);
  pair<ll, ll> punkt = {x, y};
  ll dist = numeric_limits<ll>::max();
  for (const Poziomica& poz : poziomice) {
    bool nie_zaw = !poz.Zawiera(x, y);
    for (int i = 0; i < poz.n; i++) {
      const int j = poz.Next(i);
      dist = min(dist, PointPointDistance(
          punkt, poz.punkty[i]) + poz.distance - 2 * nie_zaw);
      dist = min(dist, PointSegmentDistance(
          punkt, poz.punkty[i], poz.punkty[j]) + poz.distance - 2 * nie_zaw);
    }
  }
  //debug() << imie(dist);
  return dist;
}

void Pozamiataj() {
  ll min_x = 0, max_x = 0, min_y = 0, max_y = 0;
  bool first = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < poziomice[i].n; j++) {
      const ll x = poziomice[i].punkty[j].first;
      const ll y = poziomice[i].punkty[j].second;
      if (first) {
        min_x = max_x = x;
        min_y = max_y = y;
        first = false;
      } else {
        min_x = min(min_x, x);
        max_x = max(max_x, x);
        min_y = min(min_y, y);
        max_y = max(max_y, y);
      }
    }
  }
  if (!(min_x & 1)) min_x++;
  if (!(max_x & 1)) max_x--;
  if (!(min_y & 1)) min_y++;
  if (!(max_y & 1)) max_y--;
  while (MamSekunde()) {
    Wzarzaj(min_x, max_x, min_y, max_y);
  }
  printf("%lld\n", (ultra_wynik + 1) / 2);
}

void LiczOdleglosci() {
  Graf graf(N);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      graf.AddEdge(i, j, poziomice[i].Distance(poziomice[j]));
    }
  }
  graf.Dijkstra(0);
  for (int i = 0; i < N; i++) {
    poziomice[i].distance = graf.Odl(i);
  }
}

void Algos() {
  for (Poziomica& p : poziomice) {
    p.Init();
  }
  sort(poziomice.begin(), poziomice.end(),
       [](const Poziomica& a, const Poziomica& b) -> bool {
         return a.pole > b.pole;
       });
  assert((int) poziomice.size() == N);
  for (int i = 0; i < N; i++) {
    debug() << imie(i) imie(poziomice[i].punkty);
  }
  LiczOdleglosci();
  Pozamiataj();
}

int main() {
  poczatek = time(NULL);
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    Poziomica p;
    int k;
    scanf("%d", &k);
    assert(k % 2 == 0);
    vector<int> xs;
    for (int j = 0; j < k; j++) {
      int x;
      scanf("%d", &x);
      x *= 2;
      xs.push_back(x);
    }
    for (int j = 0; j < k; j++) {
      const int l = (j + 1) % k;
      if (j % 2 == 0) {
        p.punkty.emplace_back(xs[j], xs[l]);
      } else {
        p.punkty.emplace_back(xs[l], xs[j]);
      }
    }
    poziomice.push_back(p);
  }
  Algos();
  return 0;
}