#include <bits/stdc++.h>
using namespace std;
int n, m, l, num;
vector<int> vec[105], lan[105];
int vis[100];
void make_graph() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < lan[i].size(); j++) {
      for (int k = 1; k <= n, k != i; k++) {
        for (int l = 0; l < lan[k].size(); l++) {
          if (lan[i][j] == lan[k][l]) {
            vec[i].push_back(k);
            vec[k].push_back(i);
          }
        }
      }
    }
  }
}
void dfs(int src) {
  int u = src;
  vis[u] = 1;
  for (int i = 0; i < vec[u].size(); i++) {
    int t = vec[u][i];
    if (vis[t] == 0) dfs(t);
  }
}
int main() {
  for (int i = 0; i < 105; i++) vec[i].clear();
  memset(vis, 0, sizeof(vis));
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> l;
    for (int j = 0; j < l; j++) {
      cin >> num;
      lan[i].push_back(num);
    }
  }
  make_graph();
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      cnt++;
      dfs(i);
    }
  }
  int f = 0;
  for (int i = 1; i <= n; i++) {
    if (lan[i].size() > 0) {
      f = 1;
      break;
    }
  }
  if (f == 0) cnt++;
  cout << cnt - 1 << endl;
  return 0;
}
