How to loop gradients like this

Had the same problem a while back: