Optimal design of closed multilayer magnetic shields