#include <bits/stdc++.h>
using namespace std;
const int MAXSIZE = 30000020;
int bufpos;
char buf[MAXSIZE];
void init() {
  buf[fread(buf, 1, MAXSIZE, stdin)] = '\0';
  bufpos = 0;
}
int readint() {
  bool isneg;
  int val = 0;
  for (; !isdigit(buf[bufpos]) && buf[bufpos] != '-'; bufpos++)
    ;
  bufpos += (isneg = (buf[bufpos] == '-')) ? 1 : 0;
  for (; isdigit(buf[bufpos]); bufpos++) val = val * 10 + buf[bufpos] - '0';
  return isneg ? -val : val;
}
char readchar() {
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  return buf[bufpos++];
}
int readstr(char* s) {
  int cur = 0;
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  for (; !isspace(buf[bufpos]); bufpos++) s[cur++] = buf[bufpos];
  s[cur] = '\0';
  return cur;
}
int fa[202], cnt[101];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
void mer(int x, int y) { fa[getf(x)] = getf(y); }
int main() {
  init();
  int n = readint(), m = readint();
  for (int i = 1; i <= n + m; i++) fa[i] = i;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int k = readint();
    sum += k;
    while (k--) {
      int w = readint();
      cnt[w]++;
      mer(i, n + w);
    }
  }
  if (!sum) {
    printf("%d\n", n);
    return 0;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (!cnt[j]) continue;
      int x = getf(i), y = getf(j + n);
      if (x == y)
        continue;
      else
        fa[x] = y, ans++;
    }
  printf("%d\n", ans);
}
