#include <bits/stdc++.h>
using namespace std;
int n, m;
bool visited[1000] = {false};
vector<vector<int> > adj(1000);
void DFS(int node) {
  visited[node] = true;
  for (int i = 0; i < adj[node].size(); ++i) {
    int child = adj[node][i];
    if (!visited[child]) DFS(child);
  }
}
int main() {
  cin >> n >> m;
  int lang, lang_num;
  int ans = -1, test = 1;
  for (int i = 0; i < n; ++i) {
    cin >> lang_num;
    for (int j = 0; j < lang_num; ++j) {
      cin >> lang;
      test = 0;
      adj[lang + 100].push_back(i);
      adj[i].push_back(lang + 100);
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      DFS(i);
      ans++;
    }
  }
  if (test)
    cout << n;
  else
    cout << ans;
  return 0;
}
