3lines:
\n# Code\n\nclass Solution:\n    def minimumTeachings(_, n: int, l: List[List[int]], f: List[List[int]]) -> int:\n        q=set()\n        for a,b in f: not set
\n# Code\n```\nclass Solution:\n    def minimumTeachings(_, n: int, l: List[List[int]], f: List[List[int]]) -> int:\n        q=set()\n        for a,b in f: not set(l[a-1]).intersection(set(l[b-1]))and(q.add(a),q.add(b))\n        return len(q)-(max(c.values())if ((c:=Counter()).update(chain.from_iterable((l[h-1]for h in q))),c)[-1] else 0)\n```
['Union Find', 'Python3']