#include <bits/stdc++.h>
using namespace std;
int n, m, z, flag;
int use[101][101];
int p[101];
int find(int x) {
  if (x != p[x]) p[x] = find(p[x]);
  return p[x];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x, y; i <= n; i++) {
    scanf("%d", &x);
    p[i] = i;
    flag += x != 0;
    for (int j = 1; j <= x; j++) scanf("%d", &y), use[y][i] = 1;
  }
  for (int k = 1; k <= m; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (use[k][i] && use[k][j]) p[find(i)] = find(j);
  for (int i = 1; i <= n; i++)
    if (find(i) == i) z++;
  if (!flag) z++;
  cout << z - 1 << endl;
}
