#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
void debugarr(int* arr, int n) {
  cout << "[";
  for (int i = 0; i < n; i++) cout << arr[i] << " ";
  cout << "]" << endl;
}
int vis[1000];
int n, m;
int mat[1000][1000] = {{0}};
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int i = 0; i <= (int)n + m - 1; i++)
    if (mat[x][i]) dfs(i);
}
int main() {
  memset(vis, 0, sizeof vis);
  scanf("%d", &n);
  scanf("%d", &m);
  memset(mat, 0, sizeof mat);
  int cnt = 0;
  for (int i = 0; i <= (int)n - 1; i++) {
    int x;
    scanf("%d", &x);
    if (x == 0) {
      cnt++;
    }
    for (int j = 0; j <= (int)x - 1; j++) {
      int a;
      scanf("%d", &a);
      mat[i][n + a - 1] = 1;
      mat[n + a - 1][i] = 1;
    }
  }
  if (cnt == n) {
    cout << n << endl;
    return 0;
  }
  cnt = 0;
  for (int i = 0; i <= (int)n - 1; i++) {
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  }
  cout << cnt - 1 << endl;
  return 0;
}
