Let the distance from X to Z = $d$ km.
A’s speed = 5 kmph, B’s speed = 7 kmph. Distance X to Y = 27 km.
Time taken by B to reach Y = $\frac{27}{7}$ hours. In that time, A covers $\frac{27}{7} \times 5 = \frac{135}{7} \approx 19.29$ km.
At this moment, B turns back from Y towards A at speed 7 kmph. Relative speed between B (towards A) and A (towards B) = $5 + 7 = 12$ kmph.
Remaining distance between A and B at that moment = $27 - 19.29 = 7.71$ km.
Time taken to meet after turning = $\frac{7.71}{12} \approx 0.6425$ hours.
In this additional time, A travels $5 \times 0.6425 \approx 3.21$ km.
Thus, total distance from X to Z = $19.29 + 3.21 \approx 22.5$ km. Wait — this matches option (B). Let’s recheck carefully.
Rechecking: After B reaches Y, A has 27 - 19.29 = 7.71 km left to Y. B meets A on the way, so the distance from X to meeting point Z = distance A has already covered: $19.29 +$ distance covered in meeting time. Meeting time = $\frac{7.71}{12} \approx 0.6425$ h, so additional = $5 \times 0.6425 \approx 3.21$ km. Sum = $19.29 + 3.21 \approx 22.5$ km. Correct is $\boxed{22.5 \ \text{km}}$.