#include <bits/stdc++.h>
using namespace std;
int N, M, K, T;
int ans;
int t[1001][1001];
inline int rd(int a) {
  long long b = ((long long)rand() + 1) * ((long long)rand() + 1) *
                    ((long long)rand() + 1) % 100000007 * (rand() + 1) % a +
                1;
  return (b);
}
int fa[1001];
inline int find(int x) { return (fa[x] == x ? (x) : (fa[x] = find(fa[x]))); }
inline void link(int a, int b) { fa[b] = a; }
int bz = 0;
int main() {
  int i, j, k, l;
  scanf("%d%d", &N, &M);
  for (i = 1; i <= N; i++) {
    scanf("%d", &l);
    for (j = 1; j <= l; j++) {
      scanf("%d", &k);
      t[i][k] = 1;
      bz = 1;
    }
    fa[i] = i;
  }
  for (i = 1; i <= N; i++)
    for (j = 1; j <= N; j++)
      for (k = 1; k <= M; k++)
        if (t[i][k] == 1 && t[j][k] == 1) {
          if (find(i) != find(j)) {
            link(find(i), find(j));
            break;
          }
        }
  for (i = 1; i <= N; i++)
    if (fa[i] == i) ans++;
  printf("%d\n", ans - bz);
  return (0);
}
