#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> lng[101];
vector<int> mat[101];
bool mrk[101];
void DFS(int x) {
  int tmp;
  for (int i = 0; i < mat[x].size(); i++) {
    tmp = mat[x][i];
    if (mrk[tmp]) {
      mrk[tmp] = false;
      DFS(tmp);
    }
  }
}
int main() {
  int k, tmp, cnt = -1, check = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    for (int j = 1; j <= k; j++) {
      cin >> tmp;
      check++;
      for (int r = 0; r < lng[tmp].size(); r++) {
        mat[i].push_back(lng[tmp][r]);
        mat[lng[tmp][r]].push_back(i);
      }
      lng[tmp].push_back(i);
    }
  }
  if (check == 0) cnt++;
  memset(mrk, true, sizeof mrk);
  for (int i = 1; i <= n; i++) {
    if (mrk[i]) {
      cnt++;
      mrk[i] = false;
      DFS(i);
    }
  }
  cout << cnt << endl;
  return 0;
}
