#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > a) {
  int i, j;
  for (i = 0; i < a.size(); i++) {
    for (j = 0; j < a[i].size(); j++) {
      cout << a[i][j];
    }
    cout << " ";
  }
  cout << "\n";
}
vector<vector<int> > distinct(vector<vector<int> > person, int m) {
  int i, j, n = person.size();
  vector<vector<int> > distinctperson;
  for (i = 0; i < n; i++) {
    vector<int> p = person[i];
    for (j = 0; j < distinctperson.size(); j++) {
      vector<int> dp = distinctperson[j];
      vector<int> v(m);
      vector<int>::iterator it;
      it =
          set_intersection(p.begin(), p.end(), dp.begin(), dp.end(), v.begin());
      if (it != v.begin()) {
        it = set_union(p.begin(), p.end(), dp.begin(), dp.end(), v.begin());
        v.resize(it - v.begin());
        sort(v.begin(), v.end());
        distinctperson.erase(distinctperson.begin() + j);
        distinctperson.push_back(v);
        break;
      }
    }
    if (j == distinctperson.size()) distinctperson.push_back(p);
  }
  return distinctperson;
}
int main() {
  int n, m, i, j, k, temp;
  cin >> n >> m;
  vector<vector<int> > person;
  person.resize(n);
  for (i = 0; i < n; i++) {
    cin >> k;
    for (j = 0; j < k; j++) {
      cin >> temp;
      person[i].push_back(temp);
    }
    sort(person[i].begin(), person[i].end());
  }
  while (1) {
    int prevsize = person.size();
    person = distinct(person, m);
    int nextsize = person.size();
    if (nextsize == prevsize) break;
  }
  int totalsize = 0;
  for (i = 0; i < person.size(); i++) {
    totalsize += person[i].size();
  }
  if (totalsize > 0) cout << person.size() - 1;
  if (totalsize == 0) cout << person.size();
}
