#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
struct Node
{
char a[10],b[10],c[10];// 上下,左右,前后
int step;
}si;
map<
Node func1(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.a[6];si.a[6]=si.a[5];t2=si.a[8];si.a[8]=t1;t3=si.a[7];si.a[7]=t2;si.a[5]=t3;
t1=si.c[7];t2=si.c[8];si.c[7]=si.b[4];si.c[8]=si.b[3];
t3=si.b[8];t4=si.b[7];si.b[8]=t2;si.b[7]=1;
t5=si.c[3];t6=si.c[4];si.c[3]=t3;si.c[4]=t4;
si.b[4]=t6;si.b[3]=t5;
return si;
}
Node func2(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.a[7];si.a[7]=si.a[5];t2=si.a[8];si.a[8]=t1;t3=si.a[6];si.a[6]=t2;si.a[5]=t3;
t1=si.b[3];t2=si.b[4];si.b[3]=si.c[8];si.b[4]=si.c[7];
t3=si.c[3];t4=si.c[4];si.c[3]=t1;si.c[4]=t2;
t5=si.b[8];t6=si.b[7];si.b[8]=t3;si.b[7]=t4;
si.c[7]=t6;si.c[8]=t5;
return si;
}
Node func3(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.a[6];si.a[6]=si.a[5];t2=si.a[8];si.a[8]=t1;t3=si.a[7];si.a[7]=t2;si.a[5]=t3;
t1=si.a[4];t2=si.a[2];si.a[4]=si.c[4];si.a[2]=si.c[2];
t3=si.c[6];t4=si.c[8];si.c[6]=t1;si.c[8]=t2;
t5=si.a[8];t6=si.a[6];si.a[8]=t4;si.a[6]=t3;
si.c[2]=t5;si.c[4]=t6;
return si;
}
Node func4(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.b[7];si.b[7]=si.b[5];t2=si.b[8];si.b[8]=t1;t3=si.b[6];si.b[6]=t2;si.b[5]=t3;
t1=si.c[2];t2=si.c[4];si.c[2]=si.a[2];si.c[4]=si.a[4];
t3=si.a[8];t4=si.a[6];si.a[8]=t1;si.a[6]=t2;
t5=si.c[6];t6=si.c[8];si.c[6]=t4;si.c[8]=t3;
si.a[4]=t5;si.a[2]=t6;
return si;
}
Node func5(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.c[6];si.c[6]=si.c[5];t2=si.c[8];si.c[8]=t1;t3=si.c[7];si.c[7]=t2;si.c[5]=t3;
t1=si.b[5];t2=si.b[7];si.b[5]=si.a[1];si.b[7]=si.a[2];
t3=si.a[5];t4=si.a[6];si.a[5]=t2;si.a[6]=t1;
t5=si.b[1];t6=si.b[3];si.b[1]=t3;si.b[3]=t4;
si.a[1]=t6;si.a[2]=t5;
return si;
}
Node func6(Node si)
{
char t1,t2,t3,t4,t5,t6;
t1=si.c[7];si.c[7]=si.c[5];t2=si.c[8];si.c[8]=t1;t3=si.c[6];si.c[6]=t2;si.c[5]=t3;
t1=si.b[1];t2=si.b[3];si.b[1]=si.a[2];si.b[3]=si.a[1];
t3=si.a[5];t4=si.a[6];si.a[5]=t1;si.a[6]=t2;
t5=si.b[5];t6=si.b[7];si.b[5]=t4;si.b[7]=t3;
si.a[1]=t5;si.a[2]=t6;
return si;
}
queue <Node> q;
void bfs()
{
int i;
q.push(si);
Node now,next;
while(!q.empty())
{
now=q.front();
q.pop();
if((now.a[1]==0&&now.a[2]==0&&now.a[3]==0&&now.a[4]==0)||(now.a[5]==0&&now.a[6]==0&&now.a[7]==0&&now.a[8]==0)
||(now.b[1]==0&&now.b[2]==0&&now.b[3]==0&&now.b[4]==0)||(now.b[5]==0&&now.b[6]==0&&now.b[7]==0&&now.b[8]==0)
||(now.c[1]==0&&now.c[2]==0&&now.c[3]==0&&now.c[4]==0)||(now.c[5]==0&&now.c[6]==0&&now.c[7]==0&&now.c[8]==0))
{
printf("%d\n",now.step);
return;
}
for(i=1;i<=6;i++)
{
if(i==1)
{
next=func1();
}
else if(i==2)
{
func2();
}
else if(i==3) func3();
else if(i==4) func4();
else if(i==5) func5();
else if(i==6) func6();
}
}
printf("-1\n");
return ;
}
int main()
{
scanf("%c%c",&si.a[1],&si.a[2]);
getchar();
scanf("%c%c",&si.a[3],&si.a[4]);
getchar();
scanf("%c%c",&si.a[5],&si.a[6]);
getchar();
scanf("%c%c",&si.a[7],&si.a[8]);
getchar();
scanf("%c%c",&si.b[1],&si.b[2]);
getchar();
scanf("%c%c",&si.b[3],&si.b[4]);
getchar();
scanf("%c%c",&si.b[5],&si.b[6]);
getchar();
scanf("%c%c",&si.b[7],&si.b[8]);
getchar();
scanf("%c%c",&si.c[1],&si.c[2]);
getchar();
scanf("%c%c",&si.c[3],&si.c[4]);
getchar();
scanf("%c%c",&si.c[5],&si.c[6]);
getchar();
scanf("%c%c",&si.c[7],&si.c[8]);
si.step=0;
bfs();
return 0;
}