Correct distance calculation for disconnected nodes.