#include <bits/stdc++.h>
using namespace std;
template <class T>
void LOG(T s) {}
const int INF = (int)1e9;
const int MODULO = (int)1e9 + 7;
vector<vector<int> > adjList;
vector<int> visited;
bool ok = false;
int n, l;
void dfs(int i) {
  if (i < n) visited[i] = true;
  ok |= (adjList[i].size() > 0);
  for (int j = 0; j < adjList[i].size(); j++)
    if (adjList[i][j] >= n || visited[adjList[i][j]] == false) {
      dfs(adjList[i][j]);
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cin >> n >> l;
  adjList.resize(n + l);
  for (int i = 0; i < (int)n + l; i++) adjList[i] = vector<int>();
  visited.resize(n);
  for (int i = 0; i < (int)n; i++) visited[i] = false;
  int nol, lan;
  for (int i = 0; i < (int)n; i++) {
    cin >> nol;
    for (int j = 0; j < (int)nol; j++) {
      cin >> lan;
      lan--;
      adjList[i].push_back(lan + n);
      adjList[lan + n].push_back(i);
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++)
    if (visited[i] == false) {
      res++;
      dfs(i);
    }
  if (ok) {
    cout << res - 1 << '\n';
  } else {
    cout << n << '\n';
  }
  return 0;
}
