#include <bits/stdc++.h>
using namespace std;
int n, m, a[105][105], cnt = 0;
bool vis[105];
void dfs(int ver) {
  vis[ver] = true;
  for (int i = 0; i < n; ++i)
    if (!vis[i] and (a[ver][i] or a[i][ver])) dfs(i);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  vector<vector<int> > lang(m);
  bool exist_un_0 = false;
  for (int i = 0; i < n; ++i) {
    int cnt;
    cin >> cnt;
    if (cnt) exist_un_0 = true;
    for (int j = 0; j < cnt; ++j) {
      int cur;
      cin >> cur;
      --cur;
      lang[cur].push_back(i);
    }
  }
  memset(a, 0, sizeof(a));
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < m; ++i) {
    if (!lang[i].empty())
      for (int j = 0; j < lang[i].size() - 1; ++j) {
        int f = lang[i][j], s = lang[i][j + 1];
        a[f][s] = a[s][f] = 1;
      }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (!vis[i]) {
      ++ans;
      dfs(i);
    }
  if (exist_un_0)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
  return 0;
}
