#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000000 + 7;
long long powe(long long x, long long y) {
  x = x % mod, y = y % (mod - 1);
  long long ans = 1;
  while (y > 0) {
    if (y & 1) {
      ans = (1ll * x * ans) % mod;
    }
    y >>= 1;
    x = (1ll * x * x) % mod;
  }
  return ans;
}
void fun() {}
long long vis[1000];
vector<long long> v[101];
void dfs(long long n) {
  vis[n] = 1;
  for (auto it : v[n]) {
    if (!vis[it]) dfs(it);
  }
  return;
}
signed main() {
  fun();
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m, i, x, parent, num, j;
  cin >> n >> m;
  long long cnt = 0, cnt1 = 0;
  for (i = 0; i < n; i++) {
    cin >> num;
    if (num == 0) cnt1++;
    for (j = 1; j <= num; j++) {
      cin >> x;
      if (j == 1) {
        parent = x;
      }
      v[parent].push_back(x);
      v[x].push_back(parent);
    }
  }
  for (i = 1; i <= m; i++) {
    if (!vis[i] && v[i].size() > 0) {
      dfs(i);
      cnt++;
    }
  }
  if (cnt > 0) cnt1 += cnt - 1;
  cout << cnt1;
}
