#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int N, C, a[2][500010], len[500010], x;
struct data {
  int l, r;
} seg[1000010];
int cnt;
inline bool cmp(data A, data B) { return A.l == B.l ? A.r < B.r : A.l < B.l; }
int main() {
  N = read(), C = read();
  x = 0;
  for (int i = 1; i <= N; i++, x ^= 1) {
    len[i] = read();
    for (int j = 1; j <= len[i]; j++) a[x][j] = read();
    if (i != 1) {
      int le = min(len[i], len[i - 1]), pos = 0;
      for (int j = 1; j <= le; j++)
        if (a[x][j] == a[x ^ 1][j])
          continue;
        else {
          pos = j;
          break;
        }
      if (!pos && len[i] < len[i - 1]) {
        puts("-1");
        return 0;
      }
      if (pos)
        if (a[x][pos] > a[x ^ 1][pos])
          seg[++cnt] = (data){C + 1 - a[x][pos], C - a[x ^ 1][pos]};
        else
          seg[++cnt] = (data){0, C - a[x ^ 1][pos]},
          seg[++cnt] = (data){C + 1 - a[x][pos], C + 1};
    }
  }
  for (int i = 1; i <= cnt; i++)
    if (seg[i].l > seg[i].r) swap(seg[i].l, seg[i].r);
  sort(seg + 1, seg + cnt + 1, cmp);
  int pos = 0;
  for (int i = 1; i <= cnt; i++)
    if (pos < seg[i].l)
      break;
    else
      pos = max(pos, seg[i].r + 1);
  printf("%d\n", (pos <= C) ? pos : -1);
  return 0;
}
