JAVA SOLUTION
\nclass Solution {\n    \n    int findPar(int node, int parent[]){\n        if(node == parent[node]){\n            return node;\n        }\n        return paren
```\nclass Solution {\n    \n    int findPar(int node, int parent[]){\n        if(node == parent[node]){\n            return node;\n        }\n        return parent[node] = findPar(parent[node],parent);\n    }\n    \n    public int makeConnected(int n, int[][] connections) {\n        int parent[]=new int[n];\n        for(int i=0;i<n;i++){\n            parent[i]=i;\n        }\n        int leftWire=0;\n        int component =0;\n        int m = connections.length;\n        for(int i=0;i<m;i++){\n            int p1 =findPar(connections[i][0],parent);\n            int p2 =findPar(connections[i][1],parent);\n            if(p1 != p2){\n                parent[p1]=p2;\n            }else{\n                leftWire++;\n            }\n        }\n        for(int i=0;i<n;i++){\n            if(parent[i] == i){\n                component++;\n            }\n        }\n        return (component-1) <= leftWire?component-1:-1;\n    }\n}\n```
['Java']