#include <bits/stdc++.h>
using namespace std;
bool hasIntersection(vector<int>& v1, vector<int>& v2) {
  vector<int>::iterator first1 = v1.begin(), last1 = v1.end(),
                        first2 = v2.begin(), last2 = v2.end();
  while (first1 != last1 && first2 != last2) {
    if (*first1 < *first2)
      ++first1;
    else if (*first2 < *first1)
      ++first2;
    else
      return true;
  }
  return false;
}
int main() {
  int n, m;
  cin >> n >> m;
  int k, t, cp = 0;
  vector<int> init;
  vector<vector<int> > v(m + 1, init);
  for (int i = 1; i <= n; ++i) {
    cin >> k;
    if (k == 0) cp++;
    for (int j = 0; j < k; ++j) {
      cin >> t;
      v[t].push_back(i);
    }
  }
  int last = 0;
  while (v.size() != last) {
    last = v.size();
    for (int i = 2; i < v.size(); ++i) {
      for (int j = 1; j < i; ++j) {
        if (hasIntersection(v[i], v[j])) {
          vector<int> tp;
          set_union(v[i].begin(), v[i].end(), v[j].begin(), v[j].end(),
                    inserter(tp, tp.begin()));
          v[j] = tp;
          v.erase(v.begin() + i);
          i--;
          break;
        }
      }
    }
  }
  for (int i = 1; i < v.size(); ++i) {
    if (v[i].size() == 0) {
      v.erase(v.begin() + i);
      i--;
    }
  }
  cout << (cp == n ? n : cp + v.size() - 2) << endl;
  return 0;
}
