# Extended Euclidean Algorithm in RSA [duplicate]

I am trying to solve an RSA problem. In order to calculate $$d$$, I have to calculate $$d$$ with $$e=3$$ and $$d\cdot e\equiv1 \pmod{40}$$ Obviously the answer is $$d=27$$, but I want to solve this with the extended Euclidean algorithm.
Though I know how this works, I am stuck because in the first step of the algorithm I get $$40=3\cdot13+1$$

The remainder is 1 and the algorithm stops there. How do I get $$d=27$$ by using the extended Euclidean algorithm?

I dont have a problem solving other similar examples that dont stop in the first step. But here, because it stops in the first step of the algorithm, I get confused.