#include <iostream>
#include <queue>
#include <utility>
#include <map>
#include <string>

using namespace std;

const int dl[8][3] = {{1,4,-1},{0,2,5},{1,3,6},{2,7,-1},{0,5,-1},{1,4,6},{2,5,7},{3,6,-1}};
const int dln[8] = {2,3,3,2,2,3,3,2};


int main(void){
	int i, j, k, l, val, num;
	queue<pair<pair<string,int>,int> > que; // array, count, present location
	map<string,bool> msb;
	string str, tmp, success = "01234567";
	while(1){
		str = "";
		for(i = 0;i < 8;i++){
			cin >> val;
			if(cin.eof()){
				return 0;
			}
			str += '0' + val;
		}
		for(i = 0;i < str.size();i++){
			if(str[i] == '0'){
				break;
			}
		}
		que.push(make_pair(make_pair(str,0),i));
		while(que.size()){
			str = que.front().first.first;
			num = que.front().first.second;
			l = que.front().second;
			que.pop();
			if(msb.find(str) != msb.end()){
				continue;
			}
			if(success == str){
				break;
			}
			msb.insert(map<string,bool>::value_type(str,true));
			for(k = 0;k < dln[l];k++){
				tmp = str;
				swap(tmp[l],tmp[dl[l][k]]);
				que.push(make_pair(make_pair(tmp,num + 1),dl[l][k]));
			}
		}
		cout << num << endl;
		while(que.size()){
			que.pop();
		}
		msb.clear();
	}
}