#include <bits/stdc++.h>
int v[105][105];
int p[105];
int find(int x) {
  if (x == p[x]) {
    return x;
  }
  p[x] = find(p[x]);
  return p[x];
}
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  p[x] = y;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int **a = (int **)malloc(n * sizeof(int *));
  int k;
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      v[i][j] = 0;
    }
  }
  int flag = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    if (k != 0) flag = 1;
    a[i] = (int *)malloc(k * sizeof(int));
    for (int j = 0; j < k; j++) {
      scanf("%d", &a[i][j]);
      a[i][j]--;
      v[a[i][j]][i] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int prev = -1;
    for (int j = 0; j < n; j++) {
      if (v[i][j]) {
        if (prev == -1) {
        } else {
          if (find(j) != find(prev)) {
            merge(j, prev);
          }
        }
        prev = j;
      }
    }
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] == i) count++;
  }
  if (flag)
    printf("%d\n", count - 1);
  else
    printf("%d\n", count);
  return 0;
}
