#include <bits/stdc++.h>
using namespace std;
int fa[200];
bool visit[200];
void init() {
  for (int k = 0; k < 200; k++) fa[k] = k;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
bool Union(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  fa[a] = b;
  return true;
}
int a[200];
int main() {
  int n, m;
  int ans;
  while (cin >> n >> m) {
    memset(visit, false, sizeof(visit));
    init();
    ans = 0;
    int flag = 0;
    for (int i = 0, num; i < n; i++) {
      cin >> num;
      if (num == 0) ans++;
      for (int j = 0; j < num; j++) {
        flag = 1;
        cin >> a[j];
        visit[a[j]] = true;
        for (int z = 0; z < j; z++) Union(a[j], a[z]);
      }
    }
    for (int i = 1; i <= m; i++)
      if (visit[i] && fa[i] == i) ans++;
    cout << ans - flag << endl;
  }
  return 0;
}
