#include <bits/stdc++.h>
using namespace std;
int A[100][100];
int master[100], reached[100];
int n, m;
void dfs(int s) {
  if (reached[s]) return;
  reached[s] = 1;
  for (int i = 0; i < m; i++) {
    if (A[s][i]) {
      for (int k = 0; k < n; k++) {
        if (A[k][i]) {
          dfs(k);
        }
      }
    }
  }
}
int main() {
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++) A[i][j] = 0;
  cin >> n >> m;
  int z0 = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int aij;
      cin >> aij;
      A[i][aij - 1] = 1;
      z0++;
    }
  }
  if (z0 == 0) {
    cout << n << endl;
    return 0;
  }
  for (int i = 0; i < 100; i++) {
    master[i] = 0;
    reached[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (!reached[i])
      master[i] = 1;
    else
      continue;
    dfs(i);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += master[i];
  }
  cout << cnt - 1 << endl;
  return 0;
}
