#include <bits/stdc++.h>
using namespace std;
int n, c, l;
vector<int> d[500005];
int ng[1000005];
inline void cover(int l, int r) {
  ng[l]++;
  ng[r + 1]--;
}
int main() {
  scanf("%d%d", &n, &c);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &l);
    for (int j = 1; j <= l; j++) {
      int tmp;
      scanf("%d", &tmp);
      d[i].push_back(tmp);
    }
  }
  for (int i = 1; i < n; i++) {
    int mn = min(d[i].size(), d[i + 1].size());
    int p;
    for (p = 0; p < mn; p++)
      if (d[i][p] != d[i + 1][p]) break;
    if (p == d[i].size()) continue;
    if (p == d[i + 1].size()) {
      printf("-1\n");
      return 0;
    }
    if (d[i][p] < d[i + 1][p])
      cover(c - d[i + 1][p] + 1, c - d[i][p]);
    else {
      cover(0, c - d[i][p]);
      cover(c - d[i + 1][p] + 1, c);
    }
  }
  for (int i = 0; i < c; i++) {
    if (i) ng[i] += ng[i - 1];
    if (ng[i] == 0) {
      printf("%d\n", i);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
