#include <bits/stdc++.h>
using namespace std;
vector<int> a[200], aa[200];
queue<int> b;
int c[200], i, j, k, h, p, d, n, m, z, x, y, t, u, uu, uuu, zz;
void klk(int o) {
  for (u = 1; u <= m; u++)
    for (uu = 0; uu < aa[u].size(); uu++)
      for (uuu = 0; uuu < aa[u].size(); uuu++)
        a[aa[u][uu]].push_back(aa[u][uuu]);
}
void bfs(int l) {
  for (h = 0; h < a[b.front()].size(); h++)
    if (c[a[b.front()][h]] == 0) {
      b.push(a[b.front()][h]);
      c[a[b.front()][h]] = 1;
    }
  b.pop();
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> k;
    if (k == 0) zz++;
    for (j = 1; j <= k; j++) {
      cin >> x;
      aa[x].push_back(i);
    }
  }
  klk(1);
  p = 0;
  while (p == 0) {
    if (b.empty()) {
      z++;
      for (i = 1; i <= n; i++)
        if (c[i] == 0) {
          b.push(i);
          c[i] = 1;
          break;
        }
    }
    bfs(1);
    d = 0;
    for (i = 1; i <= n; i++)
      if (c[i] == 0) d = 1;
    if (d == 0) p = 1;
  }
  if (n < 2)
    cout << "0";
  else if (zz == n)
    cout << n;
  else
    cout << z - 1;
}
