#include <cstdio>
#include <string>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, string> sk;
int ct = 0;
int d[4] = {-4,4,-1,1};
int main(){
string s,ans;
ans = "01234567";
char a;
while(cin >> a){
s.clear();
s += a;
for(int i = 1;i < 8;i++){
cin >> a;
s += a;
}
queue<sk> m;
int t;
for(int i = 0;i < 8;i++){
if(s[i] == '0'){
t = i;
break;
}
}
m.push(sk(P(0,t),s));
while(m.size()){
sk q;
int iti;
q = m.front(); m.pop();
string tmp;
tmp = q.second;
if(tmp == ans){
printf("%d\n",q.first.first);
break;
}
for(int i = 0;i < 8;i++){
if(tmp[i] == '0'){
iti = i;
break;
}
}
for(int i = 0;i < 4;i++){
int n = iti + d[i];
if(n != q.first.second && n >= 0 && n < 8){
char ttt;
string sai;
sai = tmp;
sai[iti] = tmp[n];
sai[n] = tmp[iti];
m.push(sk(P(q.first.first+1,iti),sai));
}
}
}
}
return 0;
}