#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int f[MAXN];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  int n, m, sum;
  int arr[MAXN][MAXN];
  while (~scanf("%d %d", &n, &m)) {
    sum = 0;
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < n; ++i) {
      f[i] = i;
      scanf("%d", &arr[i][0]);
      if (arr[i][0] == 0) sum++;
      for (int j = 1; j <= arr[i][0]; ++j) {
        int tmp;
        scanf("%d", &tmp);
        arr[i][tmp] = 1;
      }
    }
    if (sum == n) {
      printf("%d\n", n);
      continue;
    }
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        for (int k = 1; k <= m; ++k) {
          if (arr[i][k] != 0 && arr[i][k] == arr[j][k]) {
            int fa = find(i);
            int fb = find(j);
            if (fa != fb) f[fb] = fa;
          }
        }
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (find(i) == i) cnt++;
    }
    printf("%d\n", cnt - 1);
  }
  return 0;
}
