#include <bits/stdc++.h>
using namespace std;
int n, k;
int A[2][1000005];
struct node {
  int L, R;
  bool operator<(const node &t) const { return L < t.L; }
};
vector<node> C;
int main() {
  scanf("%d %d", &n, &k);
  int cur = 1;
  for (int i = 1; i <= n; ++i) {
    cur ^= 1;
    scanf("%d", &A[cur][0]);
    for (int j = 1; j <= A[cur][0]; ++j) scanf("%d", &A[cur][j]);
    if (i != 1) {
      int len = min(A[cur][0], A[cur ^ 1][0]);
      for (int j = 1; j <= A[!cur][0]; ++j) {
        if (j == len + 1) {
          C.push_back((node){0, n});
          break;
        }
        int x = A[!cur][j], y = A[cur][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, sz = C.size();
  for (int i = 0; i < sz; ++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;
}
