Assume it is first down and ten in this problem. When you lose 5 yards on the sack, you are 5 yards further away. So now, it is second and 15. Then, they lose another 15 yards, so it's now third and 30. The final play is that they gain 12 yards, meaning after all the plays it is fourth down and eighteen yards to go.
In math symbols, 0 is where the first down marker is. You start at -10.
First play, the sack: -10 - 5 = -15
Second play: -15 - 15 = -30
Third play: -30 + 12 = -18.
Looks like you have to punt on fourth down :)