Optimal phase estimation in quantum networks