#include <bits/stdc++.h>
using namespace std;
int fa[105];
int a[105][105];
int find(int u) {
  if (fa[u] != u) return fa[u] = find(fa[u]);
  return fa[u];
}
int main() {
  int n, m, i, j;
  cin >> n >> m;
  int f = 0;
  for (i = 1; i <= n; i++) fa[i] = i;
  for (i = 1; i <= n; i++) {
    int k, x;
    cin >> k;
    if (k) f = 1;
    for (j = 1; j <= k; j++) {
      cin >> x;
      a[i][x] = 1;
    }
  }
  for (i = 1; i <= n; i++)
    for (j = i + 1; j <= n; j++) {
      for (int k = 1; k <= m; k++)
        if (a[i][k] && a[j][k]) {
          int fi = find(i);
          int fj = find(j);
          if (fi != fj) fa[fi] = fj;
        }
    }
  int cnt = 0;
  for (i = 1; i <= n; i++) {
    if (fa[i] == i) cnt++;
  }
  if (!f)
    cout << n << endl;
  else
    cout << cnt - 1 << endl;
}
