#include <bits/stdc++.h>
using namespace std;
void dfs(int n);
const int sz = 2 * 1000 * 100 + 10;
const long long big = (1 << 62);
ostream_iterator<int> screen(cout, " ");
vector<int> adj[1000];
int used[1000];
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a;
    for (int j = 0; j < a; j++) {
      cin >> b;
      adj[i + 200].push_back(b);
      adj[b].push_back(i + 200);
    }
  }
  int cnt = 0, f = 0;
  for (int i = 200; i < n + 200; i++)
    if (!adj[i].size())
      cnt++;
    else if (!used[i]) {
      f = 1;
      cnt++;
      dfs(i);
    }
  cout << cnt - f << endl;
  return 0;
}
void dfs(int n) {
  used[n] = 1;
  for (int i = 0; i < adj[n].size(); i++)
    if (!used[adj[n][i]]) dfs(adj[n][i]);
  return;
}
