#include <bits/stdc++.h>
using namespace std;
const long long N = 105;
long long n, m, ans = 0;
vector<long long> rt(N), sz(N), vis(N);
void initialize() {
  for (long long i = 1; i <= m; i++) {
    rt[i] = i;
    sz[i] = 1;
    vis[i] = 0;
  }
}
long long root(long long i) {
  while (rt[i] != i) {
    rt[i] = rt[rt[i]];
    i = rt[i];
  }
  return i;
}
void uni(long long a, long long b) {
  long long rta = root(a);
  long long rtb = root(b);
  if (rta == rtb) return;
  long long sza = sz[rta];
  long long szb = sz[rtb];
  if (sza >= szb) {
    rt[rtb] = rt[rta];
    sz[rta] += sz[rtb];
    sz[rtb] = 0;
  } else {
    rt[rta] = rt[rtb];
    sz[rtb] += sz[rta];
    sz[rta] = 0;
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  initialize();
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    if (x == 0) {
      ans++;
      continue;
    }
    long long fl;
    cin >> fl;
    vis[fl] = 1;
    x--;
    while (x--) {
      long long lg;
      cin >> lg;
      vis[lg] = 1;
      uni(fl, lg);
      fl = lg;
    }
  }
  bool ok = false;
  for (long long i = 1; i <= m; i++) {
    if (rt[i] == i && vis[i] == 1) {
      ans++;
      ok = true;
    }
  }
  if (ok) ans--;
  cout << ans;
  return 0;
}
