#include <bits/stdc++.h>
using namespace std;
int path[105], sze[105];
int root(int a) {
  while (a != path[a]) {
    path[a] = path[path[a]];
    a = path[a];
  }
  return a;
}
int unon(int a, int b) {
  int ra = root(a);
  int rb = root(b);
  if (ra == rb) {
    return 0;
  }
  if (sze[ra] > sze[rb]) {
    path[rb] = ra;
    sze[ra] += sze[rb];
  } else {
    path[ra] = rb;
    sze[rb] += sze[ra];
  }
  return 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    path[i] = i;
    sze[i] = 1;
  }
  vector<int> v[m + 5];
  int cnt0 = 0;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    if (!k) cnt0++;
    while (k--) {
      int x;
      cin >> x;
      v[x].push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j < v[i].size(); j++) {
      n -= unon(v[i][j - 1], v[i][j]);
    }
  }
  cout << max(cnt0, n - 1) << endl;
  return 0;
}
