#include <bits/stdc++.h>
const double eps = 1e-10;
using namespace std;
using ll = long long;
using ul = unsigned long long;
using PII = pair<int, int>;
const int NN = 201;
vector<int> gr[NN];
ll n, k;
ll m, s, x, a, b, c, d, e, q;
int answww;
int in[NN], comp[NN], sz[NN], shv[NN][NN];
const string al = "abcdefghijklmnopqrstuvwxyz";
map<char, int> ft;
bool gg[111][111];
int main() {
  scanf("%d %d", &n, &m);
  a = 1;
  for (int i = 1; i <= n + m; i++) {
    sz[i] = 1;
    comp[i] = i;
    shv[i][0] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    if (k) a = 0;
    for (int xx = 1; xx <= k; xx++) {
      scanf("%d", &b);
      int x = comp[i];
      int y = comp[b + n];
      if (x == y) continue;
      if (sz[x] < sz[y]) swap(x, y);
      for (int h = 0; h < sz[y]; h++) {
        int ver = shv[y][h];
        comp[ver] = x;
        shv[x][sz[x]++] = ver;
      }
    }
  }
  if (a) {
    printf("%d", n);
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (comp[i] == i) answww++;
  }
  printf("%d\n", answww - 1);
  return 0;
}
