#include<queue>
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define SC(x) scanf("%d",&x);
#define pr(x)  cout << x << endl;
#define mem(arr,v) memset(arr,v,sizeof arr);
#define bug(x) cout << "#x = " << x <<endl;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
typedef long long ll;
using namespace std;

//set<Node> S;
map <string,int> M;
int dir[4] = {1,-1,4,-4};

bool f(int pos) {
	return pos >= 0 && pos < 8;
}

//
void bfs() {
	queue<string> q;
	q.push("01234567");
	M["01234567"] = 0;

	while(!q.empty()) {
		string s = q.front();
		q.pop();
		int pos = 0;
		for(int j = 0 ; j < 8 ; ++j) 
			if(s[j] == '0') {
				pos = j;
				break;
			}

		for(int i = 0 ; i < 4; ++i) {
			int px = pos + dir[i];
			if(px >= 0 && px < 8 && !(px == 3 && i == 0) && !(px == 4 && i == 1) ) {
				string next = s;
				swap(next[pos],next[px]);
				if(M.find(next) == M.end()) {
					M[next] = M[s] + 1;
					q.push(next);
				}
			}
		}
	}
}

int main() {
	bfs();
	string str;
	while(getline(cin,str)) {
		str.erase(remove(str.begin(),str.end(),' '),str.end());
		pr(M[str]);
	}
}