#include <bits/stdc++.h>
using namespace std;
vector<int> v[500010];
long long sum[1000010] = {0};
int main() {
  int n, c;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      int x;
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    int j = 0;
    int currsize = v[i].size();
    int prevsize = v[i - 1].size();
    while (j < currsize && j < prevsize) {
      if (v[i][j] == v[i - 1][j]) {
        j++;
      } else if (v[i][j] > v[i - 1][j]) {
        sum[0]++;
        sum[c - v[i][j] + 1]--;
        sum[c - v[i - 1][j] + 1]++;
        sum[c]--;
        break;
      } else {
        sum[c - v[i - 1][j] + 1]++;
        sum[c - v[i][j] + 1]--;
        break;
      }
    }
    if (j == prevsize)
      sum[0]++;
    else if (j == currsize) {
      printf("-1\n");
      return 0;
    }
  }
  int flag = 0;
  for (int i = 1; i < c; i++) {
    sum[i] += sum[i - 1];
  }
  for (int i = 0; i < c; i++) {
    if (sum[i] == 1LL * (n - 1)) {
      flag = 1;
      printf("%d\n", i);
      break;
    }
  }
  if (flag == 0) {
    printf("-1\n");
  }
  return 0;
}
