#include <bits/stdc++.h>
using namespace std;
int parent[105];
int n, m;
vector<int> knower[105];
int unionFind(int a) {
  if (a != parent[a]) parent[a] = unionFind(parent[a]);
  return parent[a];
}
void unify(int a, int b) { parent[a] = b; }
int main() {
  int i, lan, tot, j, s, p1, p2;
  int allJero = true;
  int done = 0;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    parent[i] = i;
    scanf("%d", &tot);
    while (tot--) {
      scanf("%d", &lan);
      if (lan) allJero = false;
      lan--;
      knower[lan].push_back(i);
    }
  }
  for (i = 0; i < m; i++) {
    s = knower[i].size();
    for (j = 1; j < s; j++) {
      if ((p1 = unionFind(knower[i][0])) != (p2 = unionFind(knower[i][j]))) {
        unify(p1, p2);
        done++;
      }
    }
  }
  cout << n - done - !allJero << endl;
  return 0;
}
