#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > v(210);
bool visited[210];
void dfs(int node) {
  visited[node] = true;
  int len = v[node].size();
  for (int i = 0; i < len; i++) {
    if (!visited[v[node][i]]) {
      dfs(v[node][i]);
    }
  }
}
int computeNumConnectedComponents() {
  int numConnectedComponents = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      numConnectedComponents++;
      dfs(i);
    }
  }
  return numConnectedComponents;
}
int main() {
  cin >> n >> m;
  bool isAllZeroLang = true;
  memset(visited, false, sizeof visited);
  for (int i = 1; i <= n; i++) {
    int numLang;
    cin >> numLang;
    if (numLang > 0) {
      isAllZeroLang = false;
    }
    int langIdentifier;
    for (int j = 0; j < numLang; j++) {
      cin >> langIdentifier;
      v[i].push_back(langIdentifier + n);
      v[langIdentifier + n].push_back(i);
    }
  }
  if (isAllZeroLang == true) {
    cout << n << endl;
    return 0;
  }
  int cc = computeNumConnectedComponents();
  cout << cc - 1 << endl;
  return 0;
}
