#include <bits/stdc++.h>
using namespace std;
int p[300], a[300], num[300];
int find(int x) {
  if (p[x] == 0)
    return x;
  else {
    p[x] = find(p[x]);
    return p[x];
  }
}
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y) p[x] = y;
}
int main() {
  int n, m, ans, i, x, j, k;
  scanf("%d%d", &n, &m);
  ans = 0;
  for (i = 1; i <= n; ++i) {
    scanf("%d", &x);
    for (j = 1; j <= x; ++j) {
      scanf("%d", &a[j]);
      ++num[a[j]];
    }
    if (x == 0) {
      ++ans;
      continue;
    }
    for (k = 2; k <= x; ++k) merge(a[1], a[k]);
  }
  bool flag = false;
  for (i = 1; i <= m; ++i) {
    if (p[i] == 0 && num[i] > 0) {
      ++ans;
      flag = true;
    }
  }
  if (flag) --ans;
  printf("%d\n", ans);
}
