#include <bits/stdc++.h>
using namespace std;
vector<int> L[200];
vector<int> E[200];
bool seen[200];
void dfs(int node) {
  seen[node] = true;
  for (int i = 0; i < E[node].size(); i++) {
    int v = E[node][i];
    if (seen[v]) continue;
    dfs(v);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  int cc = 0;
  int emp = 0;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    if (!k) {
      seen[i] = true;
      emp++;
    }
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      L[l].push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < L[i].size(); j++) {
      int u = L[i][j];
      for (int k = 0; k < L[i].size(); k++) {
        int v = L[i][k];
        E[u].push_back(v);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (seen[i]) continue;
    cc++;
    dfs(i);
  }
  if (!cc) cc = 1;
  cout << cc - 1 + emp << endl;
  return 0;
}
