Well if Amy ran 31 miles more than Ryan, and she ran 40 miles all together, than you simply just subtract 31 from 40. Write it on paper like this:
40
- 31
-------
you cant take 1 from 0 so instead you "borrow" from the number next to it which would be the 4. you never take more than 1 from a number when borrowing. so take 1 from 4, place the 1 by the 0 now, making the 4 a 3 and 0 a 10. now you can subtract the 1 from the 10, making it a 9. then you subtract 3 from 3 and the answer you get is 9.
I hope this helped!