D-meson production in p-Pb collisions at √sNN =5.02 TeV and in pp collisions at √s =7 TeV