#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
int vis[100005];
void dfs(int x) {
  vis[x] = 1;
  int i;
  for (i = 0; i < adj[x].size(); i++)
    if (vis[adj[x][i]] == 0) dfs(adj[x][i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, i, j, k, c = 0;
  int arr[100005], pr[100005];
  memset(pr, 0, sizeof(pr));
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> k;
    if (k == 0) ++c;
    for (j = 0; j < k; j++) {
      cin >> arr[j];
      pr[arr[j]] = 1;
      if (j != 0) {
        adj[arr[j]].push_back(arr[j - 1]);
        adj[arr[j - 1]].push_back(arr[j]);
      }
    }
  }
  int f = 0;
  memset(vis, 0, sizeof(vis));
  for (i = 1; i < m + 1; i++) {
    if (pr[i] && vis[i] == 0) {
      if (f == 1) ++c;
      dfs(i);
      f = 1;
    }
  }
  cout << c;
}
