#include <bits/stdc++.h>
using namespace std;
vector<int> a[500];
bool mark[500];
int flag = 0, n, m, x = 0;
void dfs(int x) {
  mark[x] = true;
  for (int i = 0; i < a[x].size(); i++) {
    if (!mark[a[x][i]]) {
      dfs(a[x][i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  memset(mark, false, sizeof(mark));
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    for (int j = 0; j < t; j++) {
      int p;
      cin >> p;
      if (p > 0) {
        flag = 1;
      }
      a[i].push_back(n + p);
      a[n + p].push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      x++;
      dfs(i);
    }
  }
  if (flag == 0) {
    cout << x << endl;
  } else {
    cout << x - 1 << endl;
  }
  return 0;
}
