There is a game where you have an item and you can raise the level of the item from +0 to a higher value.
To do this you have to spend a Gem. First 6 upgrades are 100% successful. The 7th upgrade has a 50% chance of successfully raising the item 1 level and 50% chance of dropping it 1 level. The 8th upgrade is 50% successful just like the 7th, but it will drop the level to +0 on failure.
I'm trying to determine what is the average number of Gems I have to spend to raise the item from +0 to +8. Without loss of progress I think the average value would be 6+2*(1/50%) = 10 gems (six 100% upgrades and two 50% upgrades), but I don't know how to account for the level drop.
Let $a_k$ be the expected number of gems needed to get to level 8 if currently at level $k$.
$a_7 = 1 + \frac{1}{2} a_0$ [one gem spent and half the time you have to start over from 0]
$a_6 = 1 + \frac{1}{2} a_7 + \frac{1}{2} a_5$ [one gem spent, and half the time forward and half the time backward]
$a_5 = 1 + a_6$ [always 1 gem needed to get from level 5 to level 6]
$a_0 = 5 + a_5$ [always 5 gems needed to get from level 0 to level 5]
Solving these 4 linear equations for $a_0,a_5,a_6,a_7$ would give all the information you want.