Mr. Wor is a senior player of the RTS games such as 'StarCraft'. However, in recently, Blizzard announced that they will exit the China market. As an enthusiast of Blizzard Entertainment, Wor felt frustrated deeply and had to find some substitution. In this situation, the game 'KingZ' attracted Wor's sight.
KingZ is a Table-Real-Time-Strategy game, which means two player play against and make decisions at the same time. Now we are giving you some fundamental knowledge about this game.

Basic Game Rules
As the picture illustrate, only two players (red and blue) play this game on a $$$10 \times 10$$$ size board. It is a turn base game, which means player do decision in each round. Each player occupied some fields where some garrisons on duty have, in other words, some troops on the occupied field are there. The garrison means the number of troops on the field. In each round of game, some reinforcements will reinforce the occupied field. Depending on various types of the fields, different number of reinforcements will be added into garrison. In this game, both players make decision and make strategy at the same time during each round of the game. Finally, who defeats the opposite core first will be the winner.
Map and Items
The map is composed of $$$10 \times 10$$$ blocks which we call 'field'. There are four types of each field: Core, Keep, Lawn and Wall. Each field has it own coordinate where the left-top corner is $$$(0,0)$$$ and the right-bottom corner is $$$(10,10)$$$. Noticing that each player has only one core.
Garrison and Occupation
Dictionary: Affiliation means belong to.
Every field can be occupied by garrison except the wall. initially, lawn has no garrison and is occupied by neither of players, Keep has neutral garrison, each player occupies only core with 10 garrison. Please be noted that this is just an initial situation that will be changed as the game continue.
Once some field be occupied by someone's garrison, the right of control of this field belongs to this player. A puzzling condition shows that sometimes a field has no garrison in a round, the affiliations of this field will remain, which means the affiliation stays the same as the player who occupied this field in last round.
Battle
After the decision done, at the end of each round, if some field contains garrisons of different affiliations, they will battle.
The battle will follow the steps:
first, if there are garrisons affiliate both Blue and Red, they will battle first by doing subtraction of their number of garrisons. Then the stronger one who still has garrisons left will continue his battle.
Second, the remaining garrisons will battle with the neutral troops also by doing subtraction of their number of garrisons.
Finally, there are definitely at most one affiliation of garrison survives and occupy the field.
Formally speaking, we define the field has $$$x$$$ garrisons affiliating Blue, $$$y$$$ garrisons affiliating Red, $$$z$$$ garrisons are neutral, now do following steps:
m=min(x,y)
x=x-m
y=y-m
if (x>0) then
begin
m=min(x,z)
x=x-m
z=z-m
end
else if (y>0) then
begin
m=min(y,z)
y=y-m
z=z-m
end
Reinforcement
After the end of each round, the number of garrisons in the core will increase by 2.
After the end of each round, if the keep is occupied by any player, the number of garrisons in the keep will increase by 1. Please be noted that those keep which are still neutral would not be reinforced.
After each $$$8k$$$ end of the round, the number of garrisons on the lawn where occupied by any players will increase by 1.
Decision(Operation)
Each decision, or we call it operation, can be translated into quintuple $$$(u_x,u_y,x,v_x,v_y)$$$, which means the $$$x$$$ troops of garrison on $$$(u_x,u_y)$$$ will move directly to $$$(v_x,v_y)$$$ without any cost. You have to ensure that $$$x \le \text{rounds number}+dis(u,v)$$$ ,$$$dis(u,v)=|u_x-v_x|+|u_y-v_y|$$$, $$$0 \le x \le garrison_{u_{x},u_{y}}$$$.
Victory or Defeat
The player who defeats the opposite core first will win.
Extra Limitation
PLEASE BE NOTED THAT THESE EXTRA LIMITATIONS IN THIS PROBLEM ARE SIGNIFICANT.
In this problem, unlimited times of operation can be done in a single round. And you have to ensure that after all operation done, the number of each garrisons in occupied field are greater than zero.
Although unlimited times of operation can be done, the quadruple $$$(u_x,u_y,v_x,v_y)$$$ will exist only once in operation list.
Main Problem
Now, telling you the current situation, and no operations except reinforcement before you start operation will be done, which means before both red player and blue player start operating, both of two player do nothing, and we call this as waiting rounds. Your mission is to calculate the minimum waiting rounds, which by waiting these rounds then you can occupy all field except the wall in a single round by doing unlimited times of operation. To simplify the problem, we can easily consider that opposite does nothing throughout.
Please be noted that if the number of rounds exceed 300, then we judge the game as draw and output '-1'.
Contain two $$$10 \times 10$$$ integer matrices $$$a_{i,j},c_{i,j}$$$, first input is the integer matrix $$$a_{i,j}(-1 \le a_{i,j} \le 256)$$$ and second input is the integer matrix $$$c_{i,j}(c_{i,j} \in \{ 0,1,2,3,4,5,6,7 \})$$$.
Matrix $$$a_{i,j}$$$ meaning that:
| $$$a_{i,j}$$$ | meaning |
| $$$-1$$$ | Wall |
| $$$0$$$ | have no garrison |
| $$$\gt 0$$$ | be occupied and have $$$a_{i,j}$$$ troops of garrison |
Matrix $$$c_{i,j}$$$ meaning that:
| $$$c_{i,j}$$$ | meaning |
| 0 | no affiliation |
| 1 | our core |
| 2 | our keep |
| 3 | our lawn |
| 4 | enemy core |
| 5 | enemy keep |
| 6 | enemy lawn |
| 7 | neutral keep |
Only one integer $$$ans$$$ represent that the minimum waiting rounds that by waiting these rounds then you can occupy all field expect the wall in a single round by doing infinity operation.
Noticing that if the round exceed 300, then we judge the game as draw and output '-1'.
7 5 -1 3 15 12 8 -1 47 0 4 2 -1 61 2 39 59 2 3 -1 2 11 3 1 -1 7 1 3 3 56 3 -1 1 74 -1 -1 2 3 3 8 0 -1 2 -1 7 6 2 6 60 7 2 60 2 4 5 6 -1 6 -1 6 2 9 2 4 -1 -1 74 6 -1 4 56 10 2 4 5 -1 3 16 0 2 -1 2 2 4 33 3 3 -1 0 2 0 47 -1 10 10 54 10 -1 0 3 3 6 0 3 2 3 3 0 7 0 3 3 0 6 3 1 3 3 3 0 3 3 5 6 0 3 3 3 3 7 3 0 6 7 0 0 3 3 3 3 0 0 6 0 3 3 3 3 7 3 6 7 6 6 3 3 0 3 0 3 6 6 6 6 0 0 7 3 0 3 7 6 6 6 6 0 6 5 0 3 0 6 6 6 4 6 6 0 0 3 0 7 0 6 6 7 6 0 0 3
-1
0 0 3 2 4 0 0 0 0 0 0 0 4 4 4 0 0 0 0 0 3 2 4 9 1 0 0 0 0 0 0 3 5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 0 0 0 0 0 0 0 3 3 3 0 0 0 0 0 3 3 3 1 3 0 0 0 0 0 0 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16