#include<iostream>
#include <vector>
using namespace std;
int DistanceArry[10][10];
int Mindistance=INT_MAX;
int arry[11];
int point[2];
void DFS(int index,int value)
{
if(index==11)
{
if (value<Mindistance)
{
Mindistance=value;
return ;
}
}
else
{
for(int i=0;i<2;i++)
{
int tmp=point[i];
int addvalue=DistanceArry[point[i]][arry[index]];
point[i]=arry[index];
DFS(index+1,value+addvalue);
point[i]=tmp;
}
}
}
int main()
{
for(int i=0;i<11;i++)
{
cin>>arry[i];
}
point[0]=point[1]=0;
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(i==0)
{
DistanceArry[j][0]=DistanceArry[0][j]=(11-j)/3+(11-j)%3;
}
else
{
DistanceArry[i][j]=DistanceArry[j][i]=((j-i)/3)+(j-i)%3;
}
}
}
DistanceArry[0][0]=0;
DFS(0,0);
cout<<Mindistance<<endl;
}