/*
ID:superlo1
LANG:C++
TASK:clocks
*/
#include <stdio.h>
#include <string.h>
int ini[9];
int move[10][10] = {{},
{1,1,0,1,1,0,0,0,0},{1,1,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0},{1,0,0,1,0,0,1,0,0},
{0,1,0,1,1,1,0,1,0},{0,0,1,0,0,1,0,0,1},
{0,0,0,1,1,0,1,1,0},{0,0,0,0,0,0,1,1,1},
{0,0,0,0,1,1,0,1,1}
};
inline int calc(int num[])
{
int sum = 0, base = 1;
for(int i = 0; i <= 8; i ++)
{
sum += num[i]*base;
base *= 4;
}
return sum;
}
inline void decode(int state, int num[])
{
for(int i = 0; i < 9; i ++)
{
num[i] = state % 4;
state /= 4;
}
}
void read()
{
for(int i = 0; i < 9; i ++)
{
scanf("%d", &ini[i]);
ini[i] = (ini[i] / 3) % 4;
}
int num[9];
decode(calc(ini), num);
//for(int i = 0; i < 9; i ++) printf("%d ", num[i]);
//puts("");
}
int q[300000];
int way[300000];
int step[300000];
bool h[300000];
int close, open;
bool ok(int s)
{
if(s == 0) return true;
return false;
}
void out()
{
int cnt = 0, ans[10000], state;
state = open;
while(state > 0)
{
ans[cnt++] = step[state];
state = way[state];
}
printf("%d", ans[cnt-1]);
for(int i = cnt - 2; i >= 0; i --) printf(" %d", ans[i]);puts("");
}
bool expend(int state)
{
int num[9], nnum[9];
decode(state, num);
//system("pause");
for(int i = 1; i <= 9; i ++)
{
for(int j = 0; j < 9; j ++)
nnum[j] = (num[j] + move[i][j]) % 4;
//printf("%d:", i);
int nstate = calc(nnum);
if(h[nstate]) continue;
//for(int j = 0; j < 9; j ++) printf("%d ", nnum[j]); puts("");
h[nstate] = 1;
q[++open] = nstate;
way[open] = close;
step[open] = i;
if(ok(nstate)) return 1;
}
return 0;
}
void bfs()
{
int num[9];
close = -1, open = 0;
int temp = calc(ini);
// printf("%d\n", temp);
memset(h,0,sizeof(h));
q[0] = temp;
h[temp] = 1;
way[0] = -1;
while(close < open)
{
int size = open - close;
//printf("%d:\n", step);
while(size --)
{
temp = q[++close];
int num[9];
decode(temp, num);
//puts("flag");
//for(int j = 0; j < 9; j ++) printf("%d ", num[j]); puts(":");
if(expend(temp))
{
out();
return;
}
}
}
}
int main()
{
freopen("clocks.in","r",stdin);
freopen("clocks.out","w",stdout);
read();
bfs();
//while(1);
}