#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

string exchange(string s,int a,int b){
	char tmp = s[a];
	s[a] = s[b];
	s[b] = tmp;
	return s;
}

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int p_x(int k){
	return k / 4;
}

int p_y(int k) {
	return k % 4;
}

int main(){
	map<string,int> dp;
	string target = "01234567";
	queue<string> Q;
	Q.push(target);
	while(!Q.empty()){
	 	string s = Q.front();
	 	Q.pop();
	 	int p0 = 0;//'0'的位置 
	 	for(int i = 0;i < 8;++i){
	 		if(s[i] == '0') p0 = i;
		}
		int x = p_x(p0),y = p_y(p0);
		for(int i = 0;i < 4;++i){
			int cx = x+dx[i],cy = y+dy[i];
			if(cx >= 0 && cy >=0 && cx < 4 && cy < 4){
				int pcrt = cx*4+cy;
				string crt = exchange(s,p0,pcrt);
				if(dp.find(crt) == dp.end()){
					dp[crt] = dp[s] + 1;
					Q.push(crt);
				}
			}
		}
	}
	
	string line;
	while(getline(cin,line)){
		line.erase(remove(line.begin(),line.end(),' '), line.end());
		cout << dp[line] << endl;
	}
	return 0;
}
