#include <bits/stdc++.h>
using namespace std;
const int maxn = 500000 + 10;
const int maxv = 1e6 + 10;
inline int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) ret = ret * 10 + ch - '0';
  return ret;
}
int L, R, n, c;
int *s[maxn], len[maxn], D[maxv];
inline void ADD(int l, int r) {
  if (l > r) return;
  D[l]++;
  D[r + 1]--;
}
int main() {
  cin >> n >> c;
  for (int i = 1; i <= n; i++) {
    len[i] = read();
    s[i] = new int[len[i] + 10];
    for (int j = 1; j <= len[i]; j++) s[i][j] = read();
  }
  for (int i = 1; i < n; i++) {
    for (int p = 1;; p++) {
      if (p == len[i] + 1) {
        ADD(0, c - 1);
        break;
      }
      if (p == len[i + 1] + 1) {
        printf("-1\n");
        return 0;
      }
      if (s[i][p] < s[i + 1][p]) {
        ADD(0, c - s[i + 1][p]);
        ADD(c - s[i][p] + 1, c - 1);
        break;
      }
      if (s[i][p] > s[i + 1][p]) {
        ADD(c - s[i][p] + 1, c - s[i + 1][p]);
        break;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i <= c - 1; i++)
    if ((sum += D[i]) >= n - 1) {
      printf("%d\n", i);
      return 0;
    }
  printf("-1");
  return 0;
}
