#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
#define z now.first.find("0")
typedef pair<string,int> NODE;
int main(){
	NODE now;
	map<string,bool> done;
	string t,data;
	int x;
	while(cin >> t){
		data = t;
		for(int i=0;i<7;i++){
			cin >> t;
			data += t;
		}
		done.clear();
		queue<NODE> Q;
		Q.push(make_pair(data,0));
		while(!Q.empty()){
			now = Q.front();Q.pop();
			if( now.first ==  "01234567"){
				cout << now.second << endl;
				break;
			}
			x = z;
			if( (x+1)%4 ){
				swap(now.first[x],now.first[x+1]);
				if(!done[now.first])Q.push(make_pair(now.first,now.second+1));
				done[now.first] = true;
				swap(now.first[x],now.first[x+1]);
			}
			if( x%4 ){
				swap(now.first[x],now.first[x-1]);
				if(!done[now.first])Q.push(make_pair(now.first,now.second+1));
				done[now.first] = true;
				swap(now.first[x],now.first[x-1]);
			}
			if( x<4 ){
				swap(now.first[x],now.first[x+4]);
				if(!done[now.first])Q.push(make_pair(now.first,now.second+1));
				done[now.first] = true;
				swap(now.first[x],now.first[x+4]);
			}
			if( x>=4){
				swap(now.first[x],now.first[x-4]);
				if(!done[now.first])Q.push(make_pair(now.first,now.second+1));
				swap(now.first[x],now.first[x-4]);
			}
		}
	}
}