#include <bits/stdc++.h>
using namespace std;
int tree[4000400], lazy[4000400];
vector<int> A[1000100];
void build(int low, int high, int node) {
  if (low == high) {
    tree[node] = 0;
    return;
  }
  int mid = (low + high) / 2;
  build(low, mid, 2 * node);
  build(mid + 1, high, 2 * node + 1);
}
void update(int low, int high, int begin, int end, int node) {
  if (lazy[node]) {
    tree[node] += lazy[node];
    if (low != high) {
      lazy[2 * node] += lazy[node];
      lazy[2 * node + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (low > end || high < begin) return;
  if (low >= begin && high <= end) {
    tree[node] += 1;
    if (low != high) {
      lazy[2 * node] += 1;
      lazy[2 * node + 1] += 1;
    }
    return;
  }
  int mid = (low + high) / 2;
  update(low, mid, begin, end, 2 * node);
  update(mid + 1, high, begin, end, 2 * node + 1);
}
int query(int low, int high, int begin, int end, int node) {
  if (lazy[node]) {
    tree[node] += lazy[node];
    if (low != high) {
      lazy[2 * node] += lazy[node];
      lazy[2 * node + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (low > end || high < begin) return 0;
  if (low >= begin && high <= end) return tree[node];
  int mid = (low + high) / 2;
  return query(low, mid, begin, end, 2 * node) +
         query(mid + 1, high, begin, end, 2 * node + 1);
}
int rng[1000010];
int main() {
  ios::sync_with_stdio(false);
  int n, c;
  cin >> n >> c;
  build(0, c - 1, 1);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int a;
      cin >> a;
      A[i].push_back(a);
    }
  }
  int f = 0;
  for (int i = 0; i < n - 1; i++) {
    f = 0;
    for (int j = 0; j < min(A[i].size(), A[i + 1].size()); j++) {
      if (A[i][j] != A[i + 1][j] && !f) {
        f = 1;
        int a = A[i][j], b = A[i + 1][j];
        if (a < b) {
          update(0, c - 1, 0, c - b, 1);
          update(0, c - 1, c - a + 1, c - 1, 1);
        } else {
          update(0, c - 1, c - a + 1, c - b, 1);
        }
      }
    }
    if (!f && A[i].size() <= A[i + 1].size()) {
      update(0, c - 1, 0, c - 1, 1);
    }
  }
  for (int i = 1; i <= c; i++) rng[i] += rng[i - 1];
  for (int i = 0; i < c; i++) {
    if (query(0, c - 1, i, i, 1) == n - 1) {
      cout << i;
      return 0;
    }
  }
  cout << -1;
}
