Counting the number of different groups