When you toss the first coin, you will get expected value, from definition
$$\mathbb{E}[X] =\sum_{i=1}^\infty x_i\,p_i=x_1p_1 + x_2p_2 + \dots$$
so 1 toss with probability 1/2 + 2 tosses with prob 1/2*1/2 and so on, gives:
$$\mathbb{E}[A] = 1*\frac{1}{2} + 2*\Big(\frac{1}{2}\Big)^2 + 3*\Big(\frac{1}{2}\Big)^3+\dots$$
which gives Arithmetico-geometric sequence
with $a = 0$ and $d = 1$ the infinite sum formula is simplifies to:
$$S=\frac{b r}{(1-r)^2}$$
in this $b=1/2$ and $r=1/2$, so
$$\mathbb{E}[A] = 2$$
Now the second toss simirarly:
$$\mathbb{E}[B] = 1*\frac{1}{3} + 2*\Big(\frac{2}{3}\Big)*\Big(\frac{1}{3}\Big) +3*\Big(\frac{2}{3}\Big)^2*\Big(\frac{1}{3}\Big)+\dots$$
which again gives Arithmetico-geometric sequence
in this case $b=1/3$ and $r=2/3$ gives
$$\mathbb{E}[B] = 3$$
second tossing will happen right after the first so you can add tosses giving
$$\mathbb{E}(A)+\mathbb{E}(B)=5$$
Hereby a short python script that can validate the result:
from random import random
import numpy as np
def toss_til_heads(p):
n = 0
while True:
heads = random() < p
n += 1
if heads:
print('H', end=' ')
print(n)
return n
else:
print('T', end=' ')
NUM_TRIES = 1000
tries = np.zeros(NUM_TRIES, np.uint32)
for i in range(len(tries)):
tries[i] = toss_til_heads(1/2.) + toss_til_heads(1/3.)
print()
print(np.mean(tries))