#include <bits/stdc++.h>
using namespace std;
int pre[110];
int find(int n) {
  if (n != pre[n]) pre[n] = find(pre[n]);
  return pre[n];
}
void Union(int x, int y) { pre[find(x)] = find(y); }
struct Edge {
  int v, next;
} edge[100000];
int cnt = 1;
int head[110] = {0};
bool vis[110] = {0};
void addedge(int u, int v) {
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
void dfs(int n) {
  vis[n] = 1;
  for (int i = head[n]; i; i = edge[i].next) {
    int tmp = edge[i].v;
    if (vis[tmp] == 0) dfs(tmp);
  }
}
int main() {
  for (int i = 0; i < 110; pre[i++] = i)
    ;
  int pos[110] = {0};
  int n, m;
  cin >> n >> m;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int num;
    cin >> num;
    sum += num;
    while (num--) {
      int tmp;
      cin >> tmp;
      if (pos[tmp] == 0)
        pos[tmp] = i;
      else {
        addedge(pos[tmp], i);
        addedge(i, pos[tmp]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  if (sum)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
}
