#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1002];
int visited[1002];
void DFS(int num) {
  if (visited[num] == 1) return;
  visited[num] = 1;
  for (int i = 0; i < adj[num].size(); i++) {
    DFS(adj[num][i]);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  int p = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    if (a != 0) p = -1;
    for (int j = 1; j <= a; j++) {
      int x;
      cin >> x;
      adj[i].push_back(x + n);
      adj[x + n].push_back(i);
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      DFS(i);
      cnt++;
    }
  }
  cout << cnt + p << endl;
  return 0;
}
