#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000000 + 7;
const long long int MOD1 = 998244353;
const long long int Val9 = 1000000000;
const long long int Val6 = 1000000;
vector<long long int> vd;
void no_of_divisors(long long int n) {
  for (long long int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i) {
        vd.push_back(i);
      } else {
        vd.push_back(i);
        vd.push_back(n / i);
      }
    }
  }
}
long long int pwr(long long int x, long long int y) {
  long long int r = 1;
  while (y > 0) {
    if (y & 1) r = r * x;
    x *= x;
    y >>= 1;
  }
  return r;
}
long long int pwrm(long long int x, long long int y, long long int m) {
  x %= m;
  long long int r = 1;
  while (y > 0) {
    if (y & 1) r = r * x % m;
    x = x * x % m;
    y >>= 1;
  }
  return r;
}
int isPrime(long long int n) {
  if (n <= 1) return 0;
  if (n <= 3) return 1;
  if (n % 2 == 0 || n % 3 == 0) return 0;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return 0;
  return 1;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long int> vprime;
vector<long long int> vsqr;
void SieveOfEratosthenes(long long int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  for (long long int p = 2; p <= n; p++) {
    if (prime[p]) {
      vprime.push_back(p);
      vsqr.push_back(p * p);
    }
  }
}
bool vis[Val6] = {false};
vector<long long int> v[Val6];
vector<long long int> ans;
long long int dfs(long long int s) {
  long long int p = v[s].size();
  vis[s] = true;
  for (long long int i = (long long int)0; i < (long long int)v[s].size();
       i++) {
    if (vis[v[s][i]] == false) {
      dfs(v[s][i]);
    }
  }
  return 1;
}
void bfs(vector<long long int> v[], long long int n) {
  queue<long long int> q;
  q.push(n);
  vis[n] = true;
  long long int dist = 0;
  while (!q.empty()) {
    long long int p = q.front();
    q.pop();
    dist++;
    for (long long int i = (long long int)0; i < (long long int)v[p].size();
         i++) {
      if (vis[v[p][i]] == false) {
        q.push(v[p][i]);
        vis[v[p][i]] = true;
      }
    }
  }
}
long long int nCr(long long int n, long long int r) {
  long long int ans = 1;
  for (long long int i = (long long int)0; i < (long long int)r; i++) {
    ans *= n - i;
    ans /= i + 1;
  }
  return ans;
}
long long int n;
long long int a[Val6], b[Val6];
long long int y, q;
long long int dp[100005][2];
long long int fun(long long int sum, long long int i) {
  if (sum < 0) return -Val6;
  if (sum == 0 || i == q) return 0;
  if (dp[sum][i] != -1) return dp[sum][i];
  return dp[sum][i] = max(b[i] + fun(sum - a[i], i), fun(sum, i + 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  {
    cin >> n >> q;
    vector<long long int> w;
    long long int ans = 0, z = 0;
    for (long long int i = (long long int)0; i < (long long int)n; i++) {
      long long int a;
      cin >> a;
      if (a == 0)
        z++;
      else {
        vector<long long int> p;
        for (long long int i = (long long int)0; i < (long long int)a; i++) {
          long long int x;
          cin >> x;
          p.push_back(x);
          w.push_back(x);
        }
        if (a == 1) {
          v[p[0]].push_back(p[0]);
        } else {
          for (long long int i = (long long int)0;
               i < (long long int)p.size() - 1; i++) {
            long long int x1 = p[i], x2 = p[i + 1];
            v[x1].push_back(x2);
            v[x2].push_back(x1);
          }
        }
      }
    }
    for (long long int i = (long long int)0; i < (long long int)w.size(); i++) {
      if (!vis[w[i]]) {
        dfs(w[i]);
        ans++;
      }
    }
    if (z == 0)
      cout << ans - 1;
    else if (ans == 0)
      cout << ans + z;
    else
      cout << ans + z - 1;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}
