#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a;
bool g[100][100];
bool used[100];
int n;
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < n; i++)
    if (g[v][i] && !used[i]) dfs(i);
}
int main() {
  int m;
  cin >> n >> m;
  bool now = false;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) g[i][j] = false;
  for (int i = 0; i < n; i++) used[i] = false;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (num != 0) now = true;
    a[i].resize(num);
    for (int j = 0; j < num; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      for (int i1 = 0; i1 < a[i].size(); i1++)
        for (int j1 = 0; j1 < a[j].size(); j1++)
          if (a[i][i1] == a[j][j1]) {
            g[i][j] = true;
            g[j][i] = true;
          }
  int result = 0;
  for (int i = 0; i < n; i++)
    if (!used[i]) {
      dfs(i);
      result++;
    }
  if (!now) result++;
  cout << result - 1;
}
