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

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

int main(){
	string s;
	while(getline(cin,s)){
		string t = "";
		for(int i = 0;i < s.length();i++){
			if(s[i] != ' ') t += s.substr(i,1);
		}
		map<string,bool> used;
		queue<string> que;
		que.push(t);
		for(int i = 0;;i++){
			int siz = que.size();
			if(!siz) break;
			for(int j = 0;j < siz;j++){
				t = que.front();que.pop();
				if(t == "01234567"){
					cout << i << endl;
					goto end;
				}
				if(used[t]) continue;
				used[t] = true;
				char field[2][4];
				for(int k = 0;k < 8;k++) field[k / 4][k % 4] = t[k];
				for(int k = 0;k < 8;k++){
					if(t[k] == '0'){
						int x = k / 4,y = k % 4;
						for(int l = 0;l < 4;l++){
							int nx = x + dx[l],ny = y + dy[l];
							if(nx >= 0 && nx < 2 && ny >= 0 && ny < 4){
								s = t;
								s[k] = s[nx * 4 + ny];
								s[nx * 4 + ny] = '0';
								que.push(s);
							}
						}
					}
				}
			}
		}
		end:
		continue;
	}
	return 0;
}