#include <bits/stdc++.h>
using namespace std;
vector<int> g[300];
bool visited[300];
int n, m, k, tmp, cnt;
bool b = true;
void dfs(int x) {
  if (visited[x]) {
    return;
  }
  visited[x] = true;
  for (auto i : g[x]) {
    dfs(i);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    for (int j = 0; j < k; ++j) {
      cin >> tmp;
      g[i].push_back(n + tmp - 1);
      g[n + tmp - 1].push_back(i);
    }
    if (!g[i].empty()) {
      b = false;
    }
  }
  if (b) {
    cout << n << endl;
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      ++cnt;
      dfs(i);
    }
  }
  cout << cnt - 1;
  return 0;
}
