#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, num, x, c;
int s[1000005];
vector<int> v[500005];
void update(int x, int y) {
  int dq;
  int len = min(v[x].size(), v[y].size());
  dq = -1;
  for (int i = 0; i < len; ++i)
    if (v[x][i] != v[y][i]) {
      dq = i;
      break;
    }
  if (dq == -1) {
    if (v[x].size() > v[y].size()) return;
    s[0]++;
    s[c]--;
    return;
  } else {
    if (v[x][dq] < v[y][dq]) {
      s[0]++;
      s[c - v[y][dq] + 1]--;
      s[c - v[x][dq] + 1]++;
      s[c]--;
    } else {
      s[c - v[x][dq] + 1]++;
      s[c - v[y][dq] + 1]--;
    }
  }
}
int main() {
  scanf("%d%d", &n, &c);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &num);
    for (int j = 1; j <= num; ++j) {
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1; i < n; ++i) {
    update(i, i + 1);
  }
  for (int i = 1; i <= c; ++i) s[i] += s[i - 1];
  int ans = -1;
  for (int i = c - 1; i >= 0; --i)
    if (s[i] == n - 1) ans = i;
  printf("%d\n", ans);
  return 0;
}
