#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
int n, d[MAXN], l[MAXN], c;
vector<int> w[MAXN];
int main() {
  cin >> n >> c;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &l[i]);
    for (int j = 1; j <= l[i]; j++) {
      int tmp;
      scanf("%d", &tmp);
      w[i].push_back(tmp);
    }
  }
  for (int i = 2; i <= n; i++) {
    int now = 0;
    while (1) {
      if (now >= l[i - 1]) {
        d[0]++;
        d[c]--;
        break;
      }
      if (now >= l[i]) {
        printf("-1\n");
        return 0;
      }
      if (w[i - 1][now] > w[i][now]) {
        d[c - w[i - 1][now] + 1]++;
        d[c - w[i][now] + 1]--;
        break;
      }
      if (w[i - 1][now] < w[i][now]) {
        d[0]++;
        d[c - w[i][now] + 1]--;
        d[c - w[i - 1][now] + 1]++;
        d[c]--;
        break;
      }
      now++;
    }
  }
  int tot = 0;
  for (int i = 0; i < c; i++) {
    tot += d[i];
    if (tot == n - 1) {
      printf("%d\n", i);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
