How do Transformers Learn Implicit Reasoning?