#include <bits/stdc++.h>
using namespace std;
vector<int> v1[105];
vector<int> v2[105];
bool mark[105];
int n, m, x, s;
int fl;
queue<int> q;
void bfs(int k) {
  q.push(k);
  while (!q.empty()) {
    int al = q.front();
    q.pop();
    for (int i = 0; i < v2[al].size(); i++) {
      for (int j = 0; j < v1[v2[al][i]].size(); j++)
        if (!mark[v1[v2[al][i]][j]]) {
          mark[v1[v2[al][i]][j]] = 1;
          q.push(v1[v2[al][i]][j]);
        }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 0; j < x; j++) {
      fl = 1;
      int a;
      cin >> a;
      v1[a].push_back(i);
      v2[i].push_back(a);
    }
  }
  if (!fl)
    printf("%d\n", n);
  else {
    for (int i = 0; i < n; i++) {
      if (!mark[i]) {
        bfs(i);
        s++;
      }
    }
    printf("%d\n", s - 1);
  }
}
