#include <iostream>
#include <map>
#include <queue>
#include <cstring>
#include <math.h>
using namespace std;

int d[4] = {1,-4,4,1};


int main() {
	string p = "01234567";
	queue<string> Q;
	map<string,int> M;
	Q.push(p);
	M[p] = 0;
	while(!Q.empty()){
		string s = Q.front();Q.pop();
		int pos = s.find('0');
		for(int i = 0;i<4;i++){
			int np = pos + d[i];
			if(0<= np && np<8){
				string ss = s;
				swap(ss[pos],ss[np]);
				if(M[ss] == 0 || M[ss] > M[s] + 1){
					M[ss] = M[s] + 1;
					Q.push(ss);
				}
			}

		}
	}
	string in;
	while(getline(cin,in), in != ""){
		for(int i = 0;i<in.size();i++){
			if(in[i] == ' '){
				in.erase(i,1);
				i = 0;
			}
		}
		if(in == "01234567"){
			cout << 0 << endl;
		}
		else
			cout << M[in] << endl;
	}
	return 0;

}