Bear and Different Names

CodeForces - 771B