#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 20;
int MAX = 1000000 + 10;
const int inf = 0x3f3f3f3f;
int bt[MAXN];
int num[2][500100];
int lowbit(int k) { return k & -k; }
void modify(int k, int val) {
  while (k <= MAX) {
    bt[k] += val;
    k += lowbit(k);
  }
}
int getsum(int k) {
  int ans = 0;
  while (k > 0) {
    ans += bt[k];
    k -= lowbit(k);
  }
  return ans;
}
void addInterval(int l, int r) {
  l++, r++;
  modify(l, 1);
  modify(r + 1, -1);
}
int main() {
  int n, c, cur = 0, flag = 1;
  int lastcnt, cnt;
  scanf("%d%d", &n, &c);
  MAX = c + 5;
  scanf("%d", &cnt);
  for (int i = 0; i < cnt; ++i) scanf("%d", &num[cur][i]);
  lastcnt = cnt;
  cur ^= 1;
  for (int k = 1; k < n; ++k) {
    scanf("%d", &cnt);
    for (int i = 0; i < cnt; ++i) scanf("%d", &num[cur][i]);
    if (flag) {
      int sign = 0;
      ;
      for (int i = 0; i < min(lastcnt, cnt); ++i) {
        if (num[cur][i] > num[cur ^ 1][i]) {
          sign = 1;
          addInterval(0, c - num[cur][i]);
          addInterval(c - num[cur ^ 1][i] + 1, c - 1);
          break;
        } else if (num[cur][i] < num[cur ^ 1][i]) {
          sign = 1;
          addInterval(c - num[cur ^ 1][i] + 1, c - num[cur][i]);
          break;
        }
      }
      if (sign == 0) {
        if (lastcnt > cnt)
          flag = 0;
        else
          addInterval(0, c - 1);
      }
    }
    lastcnt = cnt;
    cur ^= 1;
  }
  int ans = -1;
  if (flag) {
    for (int i = 1; i <= c; ++i) {
      if (getsum(i) == n - 1) {
        ans = i - 1;
        break;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
