#include <bits/stdc++.h>
using namespace std;
bool o[110][110];
bool s[110];
int a[110][110];
vector<int> v;
int main() {
  int n, m, z, g, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
    for (int j = 1; j < a[i][0] + 1; j++) {
      cin >> a[i][j];
      for (int q = 0; q < i; q++)
        for (int x = 1; x < a[q][0] + 1; x++)
          if (a[q][x] == a[i][j]) {
            o[q][i] = o[i][q] = true;
          }
    }
  }
  g = 0;
  z = 0;
  v.push_back(z);
  s[0] = true;
dfs:
  for (int i = 0; i < n; i++) {
    if (o[z][i] == true || o[z][i] == true)
      if (s[i] == false) {
        z = i;
        v.push_back(z);
        s[z] = true;
        goto dfs;
      }
  }
  if (z != g)
    for (int i = 1; i < v.size(); i++)
      if (v[i] == z) {
        z = v[i - 1];
        v.push_back(z);
        s[z] = true;
        goto dfs;
      }
  if (z == g) {
    for (int i = 0; i < n; i++)
      if (o[i][g] == true || o[g][i] == true)
        if (s[i] == false) {
          z = i;
          v.push_back(z);
          s[z] = true;
          goto dfs;
        }
    for (int i = 0; i < n; i++)
      if (s[i] == false) {
        z = i;
        v.push_back(z);
        g = z;
        ans++;
        s[z] = true;
        goto dfs;
      }
  }
  int u = 0;
  for (int i = 0; i < n; i++)
    if (a[i][0] == 0) u++;
  if (u == n) ans = n;
  cout << ans << endl;
  return 0;
}
