Calculate RSA private exponent when given public exponent and the modulus factors using extended euclid
Try just expanding the parentheses. Once they're all gone, collect all the multiples of $40$ and $17$ together:

$(40 - 2 \times 17) - 1 \times (17 - 2 \times (40 - 2 \times 17)) = 1$

$1\times40 - 2 \times 17 - 1 \times 17 + 2 \times (40 - 2 \times 17) = 1$

$1\times40 - 2 \times 17 - 1 \times 17 + 2 \times 40 - 4 \times 17 = 1$

$(1 + 2)\times40 - 2  \times 17 - 1 \times 17  - 4 \times 17 = 1$

$(1 + 2)\times40 - (2 + 1 + 4) \times 17 = 1$

$3\times40 - 7 \times 17 = 1$, or

$(-7) \times 17 + 3\times 40 = 1$