#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int pre[1005], c[1005];
vector<int> a[1005];
int find(int x) { return pre[x] = (x == pre[x] ? x : find(pre[x])); }
void Union(int a, int b) {
  if (a != b) pre[a] = b;
}
int main() {
  int n, m, cnt = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) pre[i] = i;
  for (int i = 1; i <= n; i++) {
    int k, b;
    cin >> c[i];
    k = c[i];
    if (k == 0) cnt++;
    while (k--) {
      cin >> b;
      a[b].push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j < a[i].size(); j++) {
      Union(find(a[i][0]), find(a[i][j]));
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (pre[i] == i && c[i]) ans++;
  cout << max(0, ans - 1) + cnt << endl;
  return 0;
}
