Pat and Mat were practicing arithmetic. In a square grid oriented according to the cardinal directions, they assigned the following arithmetic operations to each move:

- when moving north (S) they added seven,
- when moving east (V) they subtracted four,
- when moving south (J) they divided by two,
- when moving west (Z) they multiplied by three.

For example, if Mat gave Pat the number 5 and the path S-V-J, they got 4 with correct calculations.

Which number did Pat give to Mat if for the path S-V-J-Z-Z-J-V-S the correct calculation resulted in 57?

Hint. What number did Mat have in the second-to-last step?
The task can be illustrated as a so-called arithmetic snake:

![](https://cdn.mathpix.com/cropped/2024_04_17_698df76b4228de5fb9e6g-5.jpg?height=125&width=1644&top_left_y=1011&top_left_x=206)

We will gradually fill in all intermediate results from the back:

![](https://cdn.mathpix.com/cropped/2024_04_17_698df76b4228de5fb9e6g-5.jpg?height=141&width=1648&top_left_y=1254&top_left_x=204)

Pat gave Mat the number 21.

Notes. When working backwards, we reverse the operations in each step (for example, instead of $x+7=57$, we consider $x=57-7$). With this insight, the previous solution can be written as follows:

$$
\begin{aligned}
(((((((57-7)+4) \cdot 2): 3): 3) \cdot 2)+4)-7 & = \\
=((((((50+4) \cdot 2): 3): 3) \cdot 2)+4)-7 & = \\
=(((((54 \cdot 2): 3): 3) \cdot 2)+4)-7 & = \\
& \vdots \\
=28-7 & =21 .
\end{aligned}
$$

The arithmetic snake in the square grid, i.e., Mat's actual path, looked like this:

![](https://cdn.mathpix.com/cropped/2024_04_17_698df76b4228de5fb9e6g-5.jpg?height=362&width=791&top_left_y=2169&top_left_x=632)

The highlighted field in the center is where the journey began (with the number 21), it was passed through on the fourth step (intermediate result 36), and it was also where the journey ended (result 57).