The most general metric theory of gravity yielding conserved second order equations of motion in an arbitrary number of spacetime dimensions