Python3 Solution
\n\n\nclass Solution:\n\n    def dfs(self,vis,li,com,i):\n        if(vis[i]):\n            return 0\n        vis[i]=com\n        ans=1\n        for j in li[i]:\
\n\n```\nclass Solution:\n\n    def dfs(self,vis,li,com,i):\n        if(vis[i]):\n            return 0\n        vis[i]=com\n        ans=1\n        for j in li[i]:\n            ans+=self.dfs(vis,li,com,j)\n        return ans\n\n    def maxNumEdgesToRemove(self, n: int, e: List[List[int]]) -> int:\n        ans=0\n        li=[[] for i in range(n)]\n        for l in e:\n            if(l[0]==3):\n                li[l[1]-1].append(l[2]-1)\n                li[l[2]-1].append(l[1]-1)\n        vis=[0 for i in range(n)]\n        com=0\n        for i in range(n):\n            if(vis[i]):\n                continue\n            else:\n                com+=1\n                temp=self.dfs(vis,li,com,i)\n                ans+=(temp-1)\n        lia=[[] for i in range(com)]\n        lib=[[] for i in range(com)]\n        for l in e:\n            if(l[0]==1):\n                a=vis[l[1]-1]-1\n                b=vis[l[2]-1]-1\n                if(a==b):\n                    continue\n                lia[a].append(b)\n                lia[b].append(a)\n            elif(l[0]==2):\n                a=vis[l[1]-1]-1\n                b=vis[l[2]-1]-1\n                if(a==b):\n                    continue\n                lib[a].append(b)\n                lib[b].append(a)\n        visa=[0 for i in range(com)]\n        coma=0\n        comb=0\n        for i in range(com):\n            if(visa[i]):\n                continue\n            else:\n                coma+=1\n                if(coma>1):\n                    return -1\n                self.dfs(visa,lia,coma,i)\n        \n        visb=[0 for i in range(com)]\n        for i in range(com):\n            if(visb[i]):\n                continue\n            else:\n                comb+=1\n                if(comb>1):\n                    return -1\n                self.dfs(visb,lib,comb,i)\n        if(coma>1 or comb>1):\n            return -1\n        ans+=(com-1)*2\n        return len(e)-ans\n\n\n```
['Python', 'Python3']