#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> ADJLST;
vector<int> VIS;
void dfs(int ind) {
  VIS[ind] = 1;
  for (int i = 0; i < ADJLST[ind].size(); i++) {
    int adjc = ADJLST[ind][i];
    if (VIS[adjc] == 0) {
      dfs(adjc);
    }
  }
  return;
}
int main() {
  int N, M;
  cin >> N >> M;
  ADJLST.resize(N);
  vector<vector<bool>> LANGS(N, vector<bool>(M, false));
  VIS.resize(N, 0);
  int zeroes = 0;
  for (int i = 0; i < N; i++) {
    int sz;
    cin >> sz;
    if (sz == 0)
      zeroes++;
    else {
      while (sz--) {
        int lang;
        cin >> lang;
        lang--;
        LANGS[i][lang] = true;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      bool canComm = false;
      for (int k = 0; k < M; k++) {
        if (LANGS[i][k] && LANGS[i][k] == LANGS[j][k]) {
          canComm = true;
          break;
        }
      }
      if (canComm) {
        ADJLST[i].push_back(j);
        ADJLST[j].push_back(i);
      }
    }
  }
  int countCon = 0;
  for (int i = 0; i < N; i++) {
    if (!VIS[i]) {
      countCon++;
      dfs(i);
    }
  }
  if (zeroes == N)
    cout << N;
  else
    cout << countCon - 1;
  cout << '\n';
}
