Indian National Olympiad in Informatics 2013
N people live in Sequence Land. Instead of a name, each person is identified by a sequence of integers, called his or her id. Each id is a sequence with no duplicate elements. Two people are said to be each other’s relatives if their ids have at least K elements in common. The extended family of a resident of Sequence Land includes herself or himself, all relatives, relatives of relatives, relatives of relatives of relatives, and so on without any limit.
Given the ids of all residents of Sequence Land, including its President, and the number K, find the number of people in the extended family of the President of Sequence Land.
For example, suppose N = 4 and K = 2. Suppose the President has id (4, 6, 7, 8) and the other three residents have ids (8, 3, 0, 4), (0, 10), and (1, 2, 3, 0, 5, 8). Here, the President is directly related to (8, 3, 0, 4), who in turn is directly related to (1, 2, 3, 0, 5, 8). Thus, the President’s extended family consists of everyone other than (0, 10) and so has size 3.

-----Input format-----
• Line 1: Two space-separated integers, N followed by K.
• Lines 2 to N + 1: Each line describes an id of one of the residents of Sequence Land, beginning with the President on line 2. Each line consists of an integer p, followed by p distinct integers, the id.

-----Output format-----
The output consists of a single integer, the number of people in the extended family of the President.

-----Test Data-----
The testdata is grouped into two subtasks. In both subtasks, 1 ≤ N ≤ 300 and 1 ≤ K ≤ 300. Each number in each id is between 0 and 109 inclusive.
• Subtask 1 [30 points]: The number of elements in each id is between 1 and 10 inclusive.
• Subtask 2 [70 points]: The number of elements in each id is between 1 and 300 inclusive.

-----Example-----
Here is the sample input and output corresponding to the example above.

-----Sample input -----
4 2
4 4 6 7 8
4 8 3 0 4
2 0 10
6 1 2 3 0 5 8

-----Sample output-----
3

Note: Your program should not print anything other than what is specified in the output format. Please remove all diagnostic print statements before making your final submission. A program with extraneous output will be treated as incorrect!

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'n1 = input()\n# k = int(input())\nn, k = map(int, n1.split())\nmain_lot = []\nrel_mat = []\nfor i in range(n):\n    x1 =[]\n    for j in range(n):\n        x1.append(0)\n    rel_mat.append(x1)\nfor i in range(n):\n    # print(i)\n    rel_mat[i][i] = 1\n# print(rel_mat)\nfor gen in range(n):\n    length1 = list(map(int, input().split()))\n    length1 = length1[1:]\n    # print(length1)\n    for lits in range(len(main_lot)):\n        independentcunt = 0\n        # main_lot[lits]\n        for everyele in range(len(length1)):\n            # length1[everyele]\n            if length1[everyele] in main_lot[lits]:\n                independentcunt += 1\n        # print(independentcunt)\n        if independentcunt>=k:\n            rel_mat[gen][lits] = rel_mat[lits][gen] = 1\n            pass\n    main_lot.append(length1)\n    pass\n\n\n# print(n)\n# print(k)\n\n# print(main_lot)\n# print(rel_mat)\nfami_ly = []\nunfam_ily = []\ni1te = 0\n\n\n\n\n\n\nwhile i1te<5:\n    new_ele_rel = []\n    for ele in rel_mat:\n        temp1 = []\n        for el1 in ele:\n            temp1.append(el1)\n        new_ele_rel.append(temp1)\n    i1te+=1\n    for lists1 in range(len(rel_mat)):\n        \n        unfam_ily = [i2 for i2 in range(n)]\n        fami_ly = [i1 for i1 in range(len(rel_mat[lists1])) if rel_mat[lists1][i1] == 1 ]\n        fami_ly.remove(lists1)\n        unfam_ily.remove(lists1)\n        for tem1 in fami_ly:\n            unfam_ily.remove(tem1)\n        unfami_ly = [i2 for i2 in range(len(rel_mat[lists1])) if rel_mat[lists1][i2] == 0 ]\n\n        for iter1 in fami_ly:\n            for iter2 in unfam_ily:\n                if new_ele_rel[iter1][iter2] == 1:\n                    # print("did reach")\n\n                    new_ele_rel[iter2][lists1] = 1\n                    new_ele_rel[lists1][iter2] = 1\n                    # print(new_ele_rel[lists1][iter1])\n                    # print(iter1,lists1)\n                    # print(new_ele_rel)\n\n                    pass \n                pass \n        #     pass\n        # print(fami_ly)\n        # print(unfam_ily)\n        fami_ly = []\n        unfam_ily = []\n\n        # print(rel_mat)\n\n\n\ncount = sum(new_ele_rel[0])\nprint(count)\n\n\n\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: