Z-boson production in p-Pb collisions at √sNN = 8.16 TeV and Pb-Pb collisions at √sNN = 5.02 TeV