#include <bits/stdc++.h>
using namespace std;
std::vector<int> adj[120];
bool visited[120];
std::vector<int> lang[120];
void dfs(int v) {
  visited[v] = 1;
  for (int u : adj[v]) {
    if (!visited[u]) dfs(u);
  }
}
void solve() {
  int n, m, i, j, count = 0, count2 = 0, k, x;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> k;
    if (!k) count2++;
    for (j = 0; j < k; j++) {
      cin >> x;
      lang[x].push_back(i + 1);
    }
  }
  for (i = 0; i < m + 1; i++) {
    if (!i) continue;
    int size = lang[i].size();
    for (j = 0; j < size - 1; j++) {
      adj[lang[i][j]].push_back(lang[i][j + 1]);
      adj[lang[i][j + 1]].push_back(lang[i][j]);
    }
  }
  for (i = 0; i < n; i++) {
    if (!visited[i + 1]) {
      dfs(i + 1);
      count++;
    }
  }
  cout << max(count2, count - 1) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  solve();
  return 0;
}
