#include <bits/stdc++.h>
using namespace std;
bool adj[110][110];
bool vis[110];
int n, m;
void gao(int src) {
  for (int i = 0; i < n; i++)
    if (!vis[i] && adj[src][i]) {
      vis[i] = true;
      gao(i);
    }
}
int main() {
  memset(adj, false, sizeof(adj));
  memset(vis, false, sizeof(vis));
  cin >> n >> m;
  int list[110][110];
  bool flag = true;
  for (int i = 0; i < n; i++) {
    cin >> list[i][0];
    if (list[i][0]) flag = false;
    for (int j = 1; j <= list[i][0]; j++) {
      int t;
      cin >> t;
      list[i][j] = t;
      for (int k = 0; k < i; k++)
        if (adj[i][k] == false)
          for (int l = 1; l <= list[k][0]; l++)
            if (list[k][l] == list[i][j]) {
              adj[i][k] = adj[k][i] = true;
              break;
            }
    }
  }
  if (flag) {
    cout << n << endl;
    return 0;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      cnt++;
      vis[i] = true;
      gao(i);
    }
  cout << cnt - 1 << endl;
  return 0;
}
