#include <bits/stdc++.h>
using namespace std;
const int M = 500005;
struct node {
  int L, R;
  bool operator<(const node &t) const { return L < t.L; }
};
vector<node> C;
vector<int> num[M];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    int mx;
    scanf("%d", &mx);
    for (int j = 1, x; j <= mx; ++j) {
      scanf("%d", &x);
      num[i].push_back(x);
    }
  }
  for (int i = 1; i < n; ++i) {
    int len = min((int)num[i].size(), (int)num[i + 1].size());
    for (int j = 0; j < (int)num[i].size(); ++j) {
      if (j == len) {
        C.push_back((node){0, n});
        break;
      }
      int x = num[i][j], y = num[i + 1][j];
      if (x < y) {
        int L = k - y, R = k - x;
        C.push_back((node){L + 1, R});
        break;
      } else if (x > y) {
        int L = k - x, R = k - y;
        C.push_back((node){0, L});
        C.push_back((node){R + 1, n});
        break;
      }
    }
  }
  sort(C.begin(), C.end());
  int R = -1;
  for (int i = 0; i < (int)C.size(); ++i) {
    if (C[i].L > R + 1) {
      printf("%d\n", R + 1);
      return 0;
    } else
      R = max(C[i].R, R);
  }
  if (R < n - 1) {
    printf("%d\n", R + 1);
    return 0;
  }
  printf("-1\n");
  return 0;
}
