hackerrank Connecting Towns solution