var
i,j,a,y,min:longint;
s:string;
m:array[0..5,0..5] of boolean;
o:array[0..65535] of longint;
function js:longint;
var
i,j,a:longint;
begin
a:=0;
for i:=1 to 4 do
for j:=1 to 4 do
if m[i,j] then a:=(a shl 1)+1 else a:=a shl 1;
exit(a);
end;
procedure bfs(x,t:longint);
var
i,j:longint;
begin
if t>=min then exit;
if x=y then
begin
min:=t;
exit;
end;
if t>=o[x] then exit;
o[x]:=t;
for i:=1 to 4 do
for j:=1 to 4 do
if m[i,j] then
begin
if m[i-1,j]=false then
begin
m[i-1,j]:=true;
m[i,j]:=false;
bfs(js,t+1);
m[i,j]:=true;
m[i-1,j]:=false;
end;
if m[i+1,j]=false then
begin
m[i+1,j]:=true;
m[i,j]:=false;
bfs(js,t+1);
m[i,j]:=true;
m[i+1,j]:=false;
end;
if m[i,j-1]=false then
begin
m[i,j-1]:=true;
m[i,j]:=false;
bfs(js,t+1);
m[i,j]:=true;
m[i,j-1]:=false;
end;
if m[i,j+1]=false then
begin
m[i,j+1]:=true;
m[i,j]:=false;
bfs(js,t+1);
m[i,j]:=true;
m[i,j+1]:=false;
end;
end;
end;
begin
min:=100;
for i:=0 to 5 do
begin
m[i,0]:=true;
m[i,5]:=true;
m[0,i]:=true;
m[5,i]:=true;
end;
for i:=0 to 65535 do o[i]:=1 shl 31-1;
for i:=1 to 4 do
begin
readln(s);
for j:=1 to 4 do
if s[j]='1' then m[i,j]:=true else m[i,j]:=false;
end;
readln;
for i:=1 to 4 do
begin
readln(s);
for j:=1 to 4 do
if s[j]='1' then y:=(y shl 1)+1 else y:=y shl 1;
end;
bfs(js,0);
writeln(min);
end.
dmFyCmksaixhLHksbWluOmxvbmdpbnQ7CnM6c3RyaW5nOwptOmFycmF5WzAuLjUsMC4uNV0gb2YgYm9vbGVhbjsKbzphcnJheVswLi42NTUzNV0gb2YgbG9uZ2ludDsKCmZ1bmN0aW9uIGpzOmxvbmdpbnQ7CnZhcgppLGosYTpsb25naW50OwpiZWdpbgphOj0wOw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