#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 110;
vector<int> lang[MAX_N];
int sm[MAX_N][MAX_N];
int flag[MAX_N];
int n, m;
void dfs(int v) {
  flag[v] = 1;
  for (int i = 0; i < n; i++)
    if (sm[v][i] == 1 && flag[i] == 0) dfs(i);
}
int main() {
  cin >> n >> m;
  int cnt_all = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      cnt_all++;
      lang[x].push_back(i);
    }
  }
  if (cnt_all == 0) {
    cout << n;
    return 0;
  }
  for (int i = 0; i < m + 5; i++) {
    int size = (int)lang[i].size();
    for (int x = 0; x < size; x++)
      for (int y = x + 1; y < size; y++)
        sm[lang[i][x]][lang[i][y]] = sm[lang[i][y]][lang[i][x]] = 1;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (flag[i] == 0) {
      dfs(i);
      cnt++;
    }
  cout << (cnt - 1);
  return 0;
}
