#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int map[12][12]={0};
long long num[12];
int rrow,rcol;
int xrow,xcol;
int crow,ccol;
int maps[12][12][12][12]={0};
int flag=1;
queue<int>q1;
queue<int>q2;
queue<int>q3;
queue<int>q4;
void input();
void bfs();
void pushx();
void isend();
int canpush();
int main()
{
input();
bfs();
if(flag == 1)
{
cout<<"-1"<<endl;
return 0;
}
return 0;
}
void input()
{
int i,j;
for(i=1;i<=10;i++)
{
cin>>num[i];
}
for(i = 1 ; i <= 10 ; i ++)
{
map[i][0]=1;
for(j = 10 ; j >= 1 ; j --)
{
map[i][j] = num[i] % 10;
num[i]=num[i] / 10;
}
map[i][11]=1;
}
for(i=0;i<=11;i++)
{
map[0][i]=1;
map[11][i]=1;
}
for(i = 1 ; i <= 10 ; i ++)
{
for(j = 1 ; j <= 10 ; j++)
{
if(map[i][j] == 2)
{
xrow=i;
xcol=j;
}
if(map[i][j] == 3)
{
crow=i;
ccol=j;
}
if(map[i][j] == 4)
{
rrow=i;
rcol=j;
}
}
}
q1.push(rrow);
q2.push(rcol);
q3.push(xrow);
q4.push(xcol);
maps[rrow][rcol][xrow][xcol] = 1;
}
void bfs()
{
int i,j;
while(!q1.empty() && flag==1)
{
rrow=q1.front();
rcol=q2.front();
xrow=q3.front();
xcol=q4.front();
q1.pop();
q2.pop();
q3.pop();
q4.pop();
isend();
if(canpush() && flag==1)
{
pushx();
}
}
}
void pushx()
{
int i;
for(i=0;i<4;i++)
{
if(i == 0 && map[rrow][rcol-1] != 1 )
{
if(rrow == xrow && rcol - 1 == xcol && map[rrow][rcol-2] != 1 && maps[rrow][rcol-1][xrow][xcol-1] == 0)
{
q1.push(rrow);
q2.push(rcol-1);
q3.push(xrow);
q4.push(xcol-1);
maps[rrow][rcol-1][xrow][xcol-1] = maps[rrow][rcol][xrow][xcol] + 1;
}
else
{
if((rrow != xrow || rcol - 1 != xcol ) && maps[rrow][rcol-1][xrow][xcol] == 0)
{
q1.push(rrow);
q2.push(rcol-1);
q3.push(xrow);
q4.push(xcol);
maps[rrow][rcol-1][xrow][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
}
}
if(i == 1 && map[rrow+1][rcol] != 1 )
{
if(rrow + 1 == xrow && rcol == xcol && map[rrow+2][rcol] != 1 && maps[rrow+1][rcol][xrow+1][xcol] == 0)
{
q1.push(rrow+1);
q2.push(rcol);
q3.push(xrow+1);
q4.push(xcol);
maps[rrow+1][rcol][xrow+1][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
else
{
if((rrow + 1 != xrow || rcol != xcol ) && maps[rrow+1][rcol][xrow][xcol] == 0)
{
q1.push(rrow + 1);
q2.push(rcol);
q3.push(xrow);
q4.push(xcol);
maps[rrow+1][rcol][xrow][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
}
}
if(i == 2 && map[rrow][rcol+1] != 1 )
{
if(rrow == xrow && rcol + 1 == xcol && map[rrow][rcol+2] != 1 && maps[rrow][rcol+1][xrow][xcol] == 0 && maps[rrow][rcol+1][xrow][xcol+1] == 0)
{
q1.push(rrow);
q2.push(rcol+1);
q3.push(xrow);
q4.push(xcol+1);
maps[rrow][rcol+1][xrow][xcol+1] = maps[rrow][rcol][xrow][xcol] + 1;
}
else
{
if((rrow != xrow || rcol + 1 != xcol ) && maps[rrow][rcol+1][xrow][xcol] == 0)
{
q1.push(rrow);
q2.push(rcol+1);
q3.push(xrow);
q4.push(xcol);
maps[rrow][rcol+1][xrow][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
}
}
if(i == 3 && map[rrow-1][rcol] != 1)
{
if(rrow - 1 == xrow && rcol == xcol && map[rrow-2][rcol] != 1 && maps[rrow-1][rcol][xrow-1][xcol] == 0)
{
q1.push(rrow-1);
q2.push(rcol);
q3.push(xrow-1);
q4.push(xcol);
maps[rrow-1][rcol][xrow-1][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
else
{
if((rrow - 1 != xrow || rcol != xcol) && maps[rrow-1][rcol][xrow][xcol] == 0)
{
q1.push(rrow-1);
q2.push(rcol);
q3.push(xrow);
q4.push(xcol);
maps[rrow-1][rcol][xrow][xcol] = maps[rrow][rcol][xrow][xcol] + 1;
}
}
}
}
}
int canpush()
{
int i,j;
int tmp = 0;
if(map[xrow-1][xcol] == 1)
{
tmp++;
}
if(map[xrow+1][xcol] == 1)
{
tmp++;
}
if(map[xrow][xcol-1] == 1)
{
tmp++;
}
if(map[xrow][xcol+1] == 1)
{
tmp++;
}
if(tmp == 0 || tmp == 1 || tmp == 2)
{
return 1;
}
return 0;
}
void isend()
{
int i,j;
for(i=crow-1;i<=crow+1;i++)
{
for(j=ccol-1;j<=ccol+1;j++)
{
if(maps[i][j][crow][ccol] != 0)
{
cout<<maps[i][j][crow][ccol]-1<<endl;
flag = 0;
}
}
}
}