#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> lang[102], adj[102];
bool visited[102];
void dfs(int node) {
  for (auto it : adj[node]) {
    if (!visited[it]) visited[it] = true, dfs(it);
  }
}
int main() {
  int num_components = 0, zero = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int k;
    cin >> k;
    lang[i].resize(k);
    for (auto &it : lang[i]) cin >> it;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (i == j) continue;
      for (auto it1 : lang[i])
        for (auto it2 : lang[j])
          if (it1 == it2) adj[i].push_back(j);
    }
  }
  memset(visited, false, sizeof(visited));
  for (int i = 1; i <= n; ++i) {
    if (!visited[i]) {
      visited[i] = true, dfs(i), num_components++;
    }
    if (lang[i].size() == 0) zero++;
  }
  if (zero == n)
    cout << n;
  else
    cout << num_components - 1 << endl;
}
