#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 10;
const long long int mod = 1e9 + 7;
long long int n, m;
long long int par[N], sz[N];
long long int find(long long int u) {
  return u == par[u] ? u : u = find(par[u]);
}
void un(long long int u, long long int v) {
  u = find(u);
  v = find(v);
  if (u != v) {
    par[v] = u;
    sz[u] += sz[v];
    sz[v] = 0;
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  long long int ans = 0;
  for (long long int i = 1; i <= m; ++i) sz[i] = 1, par[i] = i;
  vector<long long int> know(m + 1, 0);
  for (long long int i = 1; i <= n; ++i) {
    long long int x;
    cin >> x;
    if (x == 0) {
      ans++;
      continue;
    }
    long long int u;
    cin >> u;
    know[u] = 1;
    x--;
    while (x--) {
      long long int v;
      cin >> v;
      know[v] = 1;
      un(u, v);
      u = v;
    }
  }
  long long int f = 0;
  for (long long int i = 1; i <= m; ++i) {
    if (par[i] == i and know[i]) ans++, f = 1;
  }
  if (f) ans--;
  cout << ans << "\n";
  return 0;
}
