CMIMC 2016 Computer Science Problem 2

In concurrent computing, two processes may have their steps interwoven in an unknown order, as long as the steps of each process occur in order. Consider the following two processes:

\begin{array}{c|cc} \text{Process} & A & B \\ \hline \text{Step 1} & x \leftarrow x - 4 & x \leftarrow x - 5 \\ \text{Step 2} & x \leftarrow x \cdot 3 & x \leftarrow x \cdot 4 \\ \text{Step 3} & x \leftarrow x - 4 & x \leftarrow x - 5 \\ \text{Step 4} & x \leftarrow x \cdot 3 & x \leftarrow x \cdot 4 \\ \end{array}

One such interweaving is A 1, B 1, A 2, B 2, A 3, B 3, B 4, A 4, but A 1, A 3, A 2, A 4, B 1, B 2, B 3, B 4 is not, since the steps of A do not occur in order. We run A and B concurrently with x initially valued at 6. Find the minimal possible value of x among all interweavings.