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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define st first
#define nd second

#define dprintf(...) printf(__VA_ARGS__)

#include<bits/stdc++.h>
using namespace std;

/**
 * Intervals are left_bound-closed and right_bound-open: [L, R)
 */
template<typename Config>
class bit {
  template<typename TConfig = Config>
  struct range_updates_config {
    template<class U> static char (&test(typename U::TRangeUpdate const*))[1];
    template<class U> static char (&test(...))[2];

    template<class U = TConfig> constexpr static typename U::TRangeUpdate _neutral(typename U::TRangeUpdate const*) {
      return U::neutral_range_update();
    }
    template<class U = TConfig> constexpr static void* _neutral(...) { return 0; }

    static const bool enabled = (sizeof(test<TConfig>(0)) == 1);
    typedef decltype(_neutral<TConfig>(0)) Type;
    constexpr static Type neutral() {
      return _neutral<TConfig>(0);
    }
  };

  typedef typename Config::TData TData;
  typedef typename range_updates_config<Config>::Type TRangeUpdate;
  typedef const function<bool(const TData&)>& Predicate;

  int size;
  /** Interval represented by the node */
  vector<pair<int, int>> bounds;
  vector<TData> data;
  vector<TRangeUpdate> range_updates;

  bool __intersects(int L, int R, int idx) {
    return bounds[idx].first < R && L < bounds[idx].second;
  }

  bool __covers(int L, int R, int idx) {
    return L <= bounds[idx].first && bounds[idx].second <= R;
  }

  void __update_range_single(int idx, const TRangeUpdate& op) {
    Config::apply(op, data[idx], bounds[idx].first, bounds[idx].second);
    Config::compose_range_updates(op, range_updates[idx]);
  }

  template<class T = Config, typename enable_if<!range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {}

  template<class T = Config, typename enable_if<range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {
    if (idx < size) {
      __update_range_single(2*idx, range_updates[idx]);
      __update_range_single(2*idx + 1, range_updates[idx]);
      range_updates[idx] = Config::neutral_range_update();
    }
  }

  void __update_range(int L, int R, const TRangeUpdate& op, int idx) {
    if (!__intersects(L, R, idx)) {
      return;
    }
    if (__covers(L, R, idx)) {
      __update_range_single(idx, op);
      return;
    }

    __push_range_update(idx);
    __update_range(L, R, op, 2*idx);
    __update_range(L, R, op, 2*idx+1);

    data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
  }

  TData __query_range(int L, int R, int idx) {
    if (!__intersects(L, R, idx)) {
      return Config::neutral();
    }
    if (__covers(L, R, idx)) {
      return data[idx];
    }

    __push_range_update(idx);
    return Config::merge(
      __query_range(L, R, 2*idx),
      __query_range(L, R, 2*idx+1)
    );
  }

  /**
  * If last=1, the last matching is returned. If last=0, the first one.
  */
  int __find(int L, int R, Predicate fn, int idx, int last) {
    if (!__intersects(L, R, idx) || !fn(data[idx])) {
      return -1;
    }

    if (idx >= size) {
      return idx - size;
    }

    __push_range_update(idx);
    int preferred = __find(L, R, fn, 2*idx+last, last);
    if (preferred != -1) {
      return preferred;
    }
    return __find(L, R, fn, 2*idx+(last^1), last);
  }

public:
  bit(int _size = 0) {
    size = 1;
    while (size < _size) {
      size <<= 1;
    }
    data = vector<TData>(2*size, Config::neutral());
    range_updates = vector<TRangeUpdate>(2*size, range_updates_config<Config>::neutral());
    bounds = vector<pair<int, int>>(2*size);
    for (int i = 0; i < size; ++i) {
      bounds[i+size] = {i, i+1};
    }
    for (int i = size - 1; i >= 0; --i) {
      bounds[i] = {bounds[2*i].first, bounds[2*i+1].second};
    }
  }

  void update_range(int L, int R, const TRangeUpdate& op) {
    __update_range(L, R, op, 1);
  }

  void update_single(int pos, const TRangeUpdate& op) {
    update_range(pos, pos+1, op);
  }

  TData query_range(int L, int R) {
    return __query_range(L, R, 1);
  }

  TData query_single(int pos) {
    return query_range(pos, pos+1);
  }

  void set(int pos, TData value) {
    int idx = size + pos;
    if (range_updates_config<Config>::enabled) {
      idx = 1;
      while (idx < size) {
        __push_range_update(idx);
        idx = 2*idx + (pos >= bounds[2*idx+1].first);
      }
    }
    // Push pending operations

    data[idx] = value;
    idx >>= 1;
    while (idx > 0) {
      data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
      idx >>= 1;
    }
  }

  /** @returns -1 if no element found */
  int first_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 0);
  }

  int first_which(Predicate contain_check) {
    return first_which(0, size, contain_check);
  }

  /** @returns -1 if no element found */
  int last_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 1);
  }

  int last_which(Predicate contain_check) {
    return last_which(0, size, contain_check);
  }
};

const int MOD = 1000000007;

enum Inside { INSIDE_ABOVE, INSIDE_BELOW };

struct Event {
  PII position;
  int cid;
  enum { RIGHT, LEFT } end;
  Inside inside;

  bool operator<(const Event b) const {
    return position < b.position;
  }
};

struct Line {
  int y;
  int cid;
  Inside inside;

  bool operator<(const Line l) const {
    return y < l.y;
  }
};

struct Segment {
  PII a, b;
};

vector<PII> contours[15000];
vector<Segment> contour_segments[15000];
vector<Segment> contour_horizontal_segments[15000];
vector<Segment> contour_vertical_segments[15000];

int parent_contour[15000];
vector<int> child_contours[15000];

inline int dist(const PII& a, const PII& b) {
  return max(abs(a.st-b.st), abs(a.nd-b.nd));
}

int distance(int cid1, int cid2) {
  int result = 1e8 + 100;
  // REP(i,2) {
    for (auto& p: contours[cid1]) {
      for (auto& s: contour_horizontal_segments[cid2]) {
        int tmp = ((p.st > s.a.st) != (p.st > s.b.st)) ? abs(p.nd - s.a.nd) : dist(p, s.a);
        result = min(result, tmp);
      }

      for (auto& s: contour_vertical_segments[cid2]) {
        int tmp = ((p.nd > s.a.nd) != (p.nd > s.b.nd)) ? abs(p.st - s.a.st) : dist(p, s.a);
        result = min(result, tmp);
      }
    }

    swap(cid1, cid2);

    for (auto& p: contours[cid1]) {
      for (auto& s: contour_horizontal_segments[cid2]) {
        if ((p.st > s.a.st) != (p.st > s.b.st))
          result = min(result, abs(p.nd - s.a.nd));
      }

      for (auto& s: contour_vertical_segments[cid2]) {
        if ((p.nd > s.a.nd) != (p.nd > s.b.nd))
          result = min(result, abs(p.st - s.a.st));
      }
    }
  // }
  return result;
}

int outside_levels[15000];
int inside_levels[15000];
int distance_from_parent[15000];

const int DISALLOWED_START = 2;
const int DISALLOWED_END = 3;

typedef pair<Segment, int> SegmentEvent;
void add_segments(vector<SegmentEvent>& sgmts, int cid, int D, bool is_outer) {
  REP(i, contours[cid].size()-1) {
    PII pt1 = contours[cid][i], pt2 = contours[cid][i+1];
    if (pt1.nd == pt2.nd) {
      if (pt2.st > pt1.st) {
        sgmts.eb(Segment{pt1, pt2}, is_outer ? DISALLOWED_END : DISALLOWED_START);
      } else {
        sgmts.eb(Segment{pt2, pt1}, is_outer ? DISALLOWED_START : DISALLOWED_END);
      }
    }

    sgmts.eb(
      Segment{
        {min(pt1.st, pt2.st) - D, min(pt1.nd, pt2.nd) - D},
        {max(pt1.st, pt2.st) + D, min(pt1.nd, pt2.nd) - D}
      },
      DISALLOWED_START
    );

    sgmts.eb(
      Segment{
        {min(pt1.st, pt2.st) - D, max(pt1.nd, pt2.nd) + D},
        {max(pt1.st, pt2.st) + D, max(pt1.nd, pt2.nd) + D}
      },
      DISALLOWED_END
    );
  }
}

struct bit_config {
  typedef int TData;
  static TData neutral() {
    return 1;
  }
  static TData merge(const TData& left, const TData& right) {
    return min(left, right);
  }

  typedef int TRangeUpdate;
  static void apply(const TRangeUpdate& op, TData& value, int A, int B) {
    value += op;
  }
  static void compose_range_updates(const TRangeUpdate& outer, TRangeUpdate& inner) {
    inner += outer;
  }
  static TRangeUpdate neutral_range_update() {
    return TRangeUpdate();
  }
};

bool has_inner_square_with_distance(int cid, int D) {
  vector<SegmentEvent> sgmts;
  add_segments(sgmts, cid, D, true);
  for (int child: child_contours[cid]) {
    add_segments(sgmts, child, D - distance_from_parent[child] + 1, false);
  }

  sort(sgmts.begin(), sgmts.end(), [](const SegmentEvent& e1, const SegmentEvent& e2) {
    return e1.st.a.nd < e2.st.a.nd;
  });

  set<int> all_xs;
  for (auto& event: sgmts) {
    all_xs.insert(event.st.a.st);
    all_xs.insert(event.st.b.st);
  }
  map<int, int> mapped_xs;
  for (int x: all_xs) {
    int newx = mapped_xs.size();
    mapped_xs[x] = newx;
  }
  for (auto& event: sgmts) {
    event.st.a.st = mapped_xs[event.st.a.st];
    event.st.b.st = mapped_xs[event.st.b.st];
  }

  int X = mapped_xs.size();
  bit<bit_config> BIT(X);

  int previous_y = -1e8-10;
  for (auto& ev: sgmts) {
    if (ev.st.a.nd > previous_y){
      if (BIT.query_range(0, X) == 0) {
        return true;
      }
    }

    if (ev.nd == DISALLOWED_START) {
      BIT.update_range(ev.st.a.st, ev.st.b.st, 1);
    } else if (ev.nd == DISALLOWED_END) {
      BIT.update_range(ev.st.a.st, ev.st.b.st, -1);
    }

    previous_y = ev.st.a.nd;
  }

  return false;
}

int compute_inner(int cid) {
  int result = 0;

  int L = 0, R = 1e8+1;
  for (int c: child_contours[cid]) {
    L = max(L, distance_from_parent[c]);
  }

  while (L < R) {
    int D = (L+R+1)/2;
    if (has_inner_square_with_distance(cid, D)) {
      L = D;
    } else {
      R = D-1;
    }
  }

  return L;
}

void go(int cid) {
  vector<int> inside_children = child_contours[cid];
  for (int child: inside_children) {
    distance_from_parent[child] = distance(child, cid);
  }

  while (!inside_children.empty()) {
    int closest_child = inside_children[0];
    for (int child: inside_children) if (distance_from_parent[child] < distance_from_parent[closest_child]) {
      closest_child = child;
    }
    int d = distance_from_parent[closest_child];
    outside_levels[closest_child] = outside_levels[cid] + distance_from_parent[closest_child];
    inside_levels[cid] = d - 1;

    vector<int> tmp;
    tmp.swap(inside_children);
    for (int child: tmp) if (child != closest_child) {
      distance_from_parent[child] = min(distance_from_parent[child], d + distance(closest_child, child) - 1);
      inside_children.pb(child);
    }

    go(closest_child);
  }

  inside_levels[cid] = max(inside_levels[cid], compute_inner(cid));
}

int main() {
  int N;
  scanf("%d", &N);
  REP(i,N) {
    int K;
    scanf("%d", &K);

    vector<int> xs;
    REP(j,K) {
      int x;
      scanf("%d", &x);
      xs.pb(x);
    }
    xs.pb(xs[0]);
    REP(j,K) {
      if (j%2 == 0) contours[i].pb({xs[j], xs[j+1]});
      else contours[i].pb({xs[j+1], xs[j]});
    }
    contours[i].pb(contours[i][0]);
    REP(j,K) {
      if (contours[i][j].st == contours[i][j+1].st)
        contour_vertical_segments[i].pb({contours[i][j], contours[i][j+1]});
      else
        contour_horizontal_segments[i].pb({contours[i][j], contours[i][j+1]});
    }
  }

  vector<Event> events;
  REP(i,N)REP(j,contours[i].size()-1) {
    if (contours[i][j].nd == contours[i][j+1].nd) {
      if (contours[i][j+1].st > contours[i][j].st) {
        events.pb({contours[i][j], i, Event::LEFT, INSIDE_ABOVE});
        events.pb({contours[i][j+1], i, Event::RIGHT, INSIDE_ABOVE});
      } else {
        events.pb({contours[i][j], i, Event::RIGHT, INSIDE_BELOW});
        events.pb({contours[i][j+1], i, Event::LEFT, INSIDE_BELOW});
      }
    }
  }

  REP(i,N) parent_contour[i] = -1;
  sort(events.begin(), events.end());
  set<Line> segments;
  int outer_cid;
  for (auto& e: events) {
    PII p = e.position;
    Line l{p.nd, e.cid, e.inside};

    if (parent_contour[e.cid] == -1) {
      auto it = segments.lower_bound(l);
      if (it == segments.begin()) {
        outer_cid = e.cid;
        parent_contour[e.cid] = e.cid;
      } else {
        --it;
        if (it->inside == INSIDE_ABOVE) {
          parent_contour[e.cid] = it->cid;
        } else {
          parent_contour[e.cid] = parent_contour[it->cid];
        }
      }
    }

    if (e.end == Event::RIGHT) {
      segments.erase({p.nd, e.cid, e.inside});
    } else {
      segments.insert({p.nd, e.cid, e.inside});
    }
  }

  REP(i,N) if (parent_contour[i] != i) {
    child_contours[parent_contour[i]].pb(i);
  }

  outside_levels[outer_cid] = 1;
  go(outer_cid);

  int result = 0;
  REP(i,N) result = max(result, outside_levels[i] + inside_levels[i]);
  printf("%d\n", result);
}