#include <bits/stdc++.h>
const int MAXN = 110;
int n, m;
int record[110][110];
int p[MAXN];
int find(int u) { return u == p[u] ? u : p[u] = find(p[u]); }
bool vis[110];
int main() {
  scanf("%d%d", &n, &m);
  int i, j;
  for (i = 0; i <= n; i++) p[i] = i;
  memset(record, 0, sizeof(record));
  int total;
  int ind;
  bool judge = true;
  for (i = 1; i <= n; i++) {
    scanf("%d", &total);
    if (total) judge = false;
    for (j = 1; j <= total; j++) {
      scanf("%d", &ind);
      record[ind][0]++;
      record[ind][record[ind][0]] = i;
    }
  }
  int cnt;
  for (i = 1; i <= m; i++) {
    cnt = 0;
    for (j = 2; j <= record[i][0]; j++) {
      int x = find(record[i][1]);
      int y = find(record[i][j]);
      if (x != y) {
        p[y] = x;
      }
    }
  }
  int ans = 0;
  for (i = 1; i <= n; i++) {
    int x = find(i);
    if (!vis[x]) ans++;
    vis[x] = true;
  }
  if (!judge)
    printf("%d\n", ans - 1);
  else
    printf("%d\n", ans);
  return 0;
}
