#include <bits/stdc++.h>
using namespace std;
int n, m;
int lan[101][101], men[101][101], mvis[101], lvis[101];
void dfs(int i) {
  int j, k, p, q;
  mvis[i] = 1;
  for (j = 1; j <= m; j++) {
    if (lvis[j] == 0 && men[i][j]) {
      lvis[j] = 1;
      for (p = 1; p <= n; p++)
        if (lan[j][p] && mvis[p] == 0) dfs(p);
    }
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int i, j, cnt = 0, k, p, fg = 1;
  cin >> n >> m;
  for (i = 0; i < 101; i++)
    for (j = 0; j < 101; j++) lan[i][j] = men[i][j] = 0;
  for (i = 0; i < 101; i++) mvis[i] = lvis[i] = 0;
  for (i = 1; i <= n; i++) {
    cin >> k;
    while (k--) {
      cin >> j;
      lan[j][i] = 1;
      men[i][j] = 1;
      fg = 0;
    }
  }
  for (i = 1; i <= n; i++) {
    if (mvis[i] == 0) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1 + fg << endl;
  return 0;
}
