Work done against gravity for taking a mass 'm' to a height 'h' = mgh (Where 'g' is the acceleration due to gravity) .....(1)
In the given question, the mass of man X and man Y are the same and they also go to the same height of the building. So, it does not matter what path they take to reach the top of the building. According to equation (1), the total work done by both of them will always be the same.