#include <bits/stdc++.h>
using namespace std;
int n, c;
vector<pair<int, int> > ruim;
void testa(vector<int> a, vector<int> b) {
  int id = -1;
  for (int(i) = (0); (i) < (min(((int)(a).size()), ((int)(b).size()))); (i)++)
    if (a[i] != b[i]) {
      id = i;
      break;
    }
  if (id == -1) {
    if (((int)(b).size()) < ((int)(a).size()))
      ruim.push_back(make_pair(0, c - 1));
    return;
  }
  int d1 = c - a[id], d2 = c - b[id] + 1;
  if (a[id] > b[id]) {
    ruim.push_back(make_pair(0, d1));
    ruim.push_back(make_pair(d2, c - 1));
  } else
    ruim.push_back(make_pair(d2, d1));
}
vector<int> vet[505050];
int main() {
  scanf("%d%d", &n, &c);
  for (int(i) = (0); (i) < (n); (i)++) {
    int m;
    scanf("%d", &m);
    vet[i].resize(m);
    for (int(j) = (0); (j) < (m); (j)++) scanf("%d", &vet[i][j]);
  }
  for (int(i) = (0); (i) < (n - 1); (i)++) testa(vet[i], vet[i + 1]);
  sort((ruim).begin(), (ruim).end());
  int prox = 0;
  for (int(i) = (0); (i) < (((int)(ruim).size())); (i)++) {
    if (ruim[i].first > prox) {
      printf("%d\n", prox);
      return 0;
    }
    prox = max(prox, ruim[i].second + 1);
  }
  if (prox < c)
    printf("%d\n", prox);
  else
    puts("-1");
}
