#include <iostream>
#include <string>
#include <queue>
#include <map>
#define I64 long long int
#define FOR(a,start,end) for(I64 a=(start);a<(end);a++)
#define FORn(a,start,end,n) for(I64 a=(start);a<(end);a+=(n))
using namespace std;
typedef pair<string, int> P;
class SevenPuzzle{
	string board;
	string match;
public:
	SevenPuzzle(string str) : board(str){ match = "01234567"; }
	int bfs();
};
int SevenPuzzle::bfs(){
	int vi[4] = {0, 0, -1, 1}, vj[4] = {-1, 1, 0, 0};
	queue<P> que;
	que.push(P(board, 0));
	map<string, int> d;
	while(!que.empty()){
		P p = que.front();
		que.pop();
		if(d[p.first]) continue;
		bool flag = true;
		FOR(i,0,8)
			if(p.first[i] != match[i]) flag = false;
		if(flag) return p.second;
		d[p.first] = p.second;
		int pz, pi, pj, nz, ni, nj;
		FOR(i,0,8){
			if(p.first[i] == '0'){
				pz = i;
				pi = i/4;
				pj = i%4;
				break;
			}
		}
		FOR(i,0,4){
			string str = p.first;
			ni = pi + vi[i];
			nj = pj + vj[i];
			nz = ni * 4 + nj;
			if(ni >= 0 && ni <= 1 && nj >= 0 && nj <= 3){
				//ツ青板篠堋禿シツづェツ妥鳴つヲ
				str[pz] = str[nz];
				str[nz] = '0';	
				que.push(P(str, p.second+1));
			}
		}
	}
}
int main(){
	string temp, str;
	while(getline(cin, temp)){
		FORn(i,0,15,2)
			str += temp[i];
		SevenPuzzle obj(str);
		cout << obj.bfs() << endl;
		str.clear();
	}
}