Let C = Location where the first car pass 6 minutes before the second
t1 = time for the first car to travel from city A to C
$t_1 = \dfrac{x}{50}$
t2 = time for the second car to travel from city B to C
$t_2 = \dfrac{40 - x}{40} = 1 - \dfrac{x}{40}$
$t_1 = t_2 + \dfrac{30}{60} - \dfrac{6}{60}$
$\dfrac{x}{50} = \left( 1 - \dfrac{x}{40} \right) + \dfrac{2}{5}$
$\dfrac{9}{200}x = \dfrac{7}{5}$
$x = 31.11 ~ \text{km from city A}$ answer