#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int xx[5]={0,0,1,-1},
yy[5]={1,-1,0,0};
int bg,ed,h,x,y;
int t,w=1;
bool ans[5][5],mark[100010];
char ch[5];
struct data
{
bool a[5][5];
int step;
}q[100010];
int hash(bool a[5][5])
{
int k=1,s=0;
for (int i=1;i<=4;i++)
for (int j=1;j<=4;j++)
{
s+=a[i][j]*k;
k<<=1;
}
return s;
}
int main()
{
for (int i=1;i<=4;i++)
{
scanf("%s",ch);
for (int j=1;j<=4;j++)
q[0].a[i][j]=ch[j-1]-'0';
}
for (int i=1;i<=4;i++)
{
scanf("%s",ch);
for (int j=1;j<=4;j++)
ans[i][j]=ch[j-1]-'0';
}
bg=hash(q[t].a);
ed=hash(ans);
if (bg==ed)
{
printf("0");
return 0;
}
mark[bg]=1;
while (t<w)
{
for (int i=1;i<=4;i++)
for (int j=1;j<=4;j++)
if (q[t].a[i][j])
for (int k=0;k<4;k++)
{
x=xx[k]+i;
y=yy[k]+j;
if (q[t].a[x][y] || x>4 || y>4 ||x<1 || y<1)
continue;
swap(q[t].a[i][j],q[t].a[x][y]);
h=hash(q[t].a);
if (!mark[h])
{
if (h==ed)
{
printf("%d",q[t].step+1);
return 0;
}
mark[h]=1;
memcpy(q[w].a,q[t].a,sizeof(q[w].a));
q[w].step=q[t].step+1;
w++;
}
swap(q[t].a[i][j],q[t].a[x][y]);
}
t++;
}
return 0;
}