#include <bits/stdc++.h>
using namespace std;
int ri() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int ret = 0;
  while (c >= '0' && c <= '9') {
    ret = 10 * ret + c - 48;
    c = getchar();
  }
  return ret;
}
int const ex = 1e2 + 5;
int vis[100005];
int n, m, num, x;
vector<int> gr[100005];
int minu = 0;
void dfs(int x, int pa) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int j = (0); j < (gr[x].size()); ++j) {
    int u = gr[x][j];
    if (u == pa) continue;
    dfs(u, x);
  }
}
int main() {
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) {
    cin >> num;
    for (int j = (0); j < (num); ++j) {
      cin >> x;
      x += n;
      gr[i + 1].push_back(x);
      gr[x].push_back(i + 1);
    }
  }
  int cnt = -1;
  map<int, int> mo;
  for (int i = (1); i < (n + m + 1); ++i)
    if (!vis[i]) {
      if (i <= n) mo[gr[i].size()]++;
      if (i > n && gr[i].size() == 0) continue;
      dfs(i, -1);
      cnt++;
    }
  if (mo[0] == n) cnt++;
  cout << cnt << "\n";
}
