// 34'18"
#include <bits/stdc++.h>
using namespace std;
int arr[3][3];
int open[3][3];
map<int,int> hash;
int firx = 1, firy = 1, firv = 1;
void init()
{
::hash[6] = 10000, ::hash[7] = 36, ::hash[8] = 720, ::hash[9] = 360;
::hash[10] = 80, ::hash[11] = 252, ::hash[12] = 108, ::hash[13] = 72;
::hash[14] = 54, ::hash[15] = 180, ::hash[16] = 72, ::hash[17] = 180;
::hash[18] = 119, ::hash[19] = 36, ::hash[20] = 306, ::hash[21] = 1080;
::hash[22] = 144, ::hash[23] = 1800, ::hash[24] = 3600;
}
void input()
{
init();
bool flag[10];
for(int i = 0; i < 10; ++ i) flag[i] = false;
for(int i = 0; i < 3; ++ i)
{
for(int j = 0; j < 3; ++ j)
{
cin >> arr[i][j];
flag[arr[i][j]] = true;
if(arr[i][j] == 0) firx = i, firy = j, open[i][j] = true;
}
}
for(int i = 1; i < 10; ++ i)
if(!flag[i]) arr[firx][firy] = i, firv = i;
for(int i = 0; i < 3; ++ i)
{
int x, y;
cin >> x >> y;
cout << arr[-- x][-- y] << endl;
open[x][y] = true;
}
}
int add(int i)
{
if(i <= 3)
{
int res = 0;
for(int j = 0; j < 3; ++ j)
res += arr[i - 1][j];
return res;
}
else if(i <= 6 && i > 3)
{
int res = 0;
for(int j = 0; j < 3; ++ j)
res += arr[j][i - 4];
return res;
}
else if(i == 7) return arr[0][0] + arr[1][1] + arr[2][2];
else if(i == 8) return arr[0][2] + arr[1][1] + arr[2][0];
return -114514;
}
int main()
{
input();
int c;
cin >> c;
int res = add(c);
cout << ::hash[res] << endl;
return 0;
}