#include <bits/stdc++.h>
using namespace std;
int n;
int m;
vector<int> langs[101];
int e_group[101];
int l_group[101];
void combine(int a, int b) {
  for (int i = 0; i < n; ++i) {
    if (e_group[i] == b) e_group[i] = a;
  }
  for (int i = 0; i < m; ++i) {
    if (l_group[i] == b) l_group[i] = a;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    langs[i].reserve(k);
    for (int j = 0; j < k; ++j) {
      int b;
      cin >> b;
      langs[i].push_back(--b);
    }
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) sum += langs[i].size();
  if (!sum) {
    cout << n << endl;
    return 0;
  }
  for (int i = 0; i < n; ++i) e_group[i] = i;
  for (int i = 0; i < m; ++i) l_group[i] = -1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < langs[i].size(); ++j) {
      int& l = l_group[langs[i][j]];
      if (l != e_group[i]) {
        if (l == -1)
          l = e_group[i];
        else
          combine(e_group[i], l);
      }
    }
  }
  sort(e_group, e_group + n);
  int cnt = 0;
  for (int i = 1; i < n; ++i) {
    cnt += (e_group[i] != e_group[i - 1]);
  }
  cout << cnt << endl;
}
