/*
input:
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
ouput:
0
1
28
*/

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

using namespace std;

typedef pair<string, int> P;

const int INF = 100000000;

//&#36755;入
string a;

//移&#21160;方向
int op[4] = {-1, 1, -4, 4};

map<string, int> dp;						//保存从string&#21464;到"01234567"的int

//&#35745;算从"01234567"&#36716;&#25442;到其他序列所需的最小&#27493;数
void bfs(){
	queue<P> que;
	que.push(P("01234567", 0));
	dp["01234567"] = 0;
	while(!que.empty()){
		P p = que.front();
		que.pop();
		string s = p.first;
		int cur = p.second;
		for(int i = 0; i < 4; i ++){
			//&#26500;造下一次交&#25442;
			int next = cur + op[i];
			string str = s;
			swap(str[cur], str[next]);
			map<string, int>::iterator it = dp.find(str);
			//判断是否可移&#21160;以及是否&#35775;&#38382;&#36807;
			if(0 <= next && next < 8 
				&& !(cur == 3 && next == 4) && !(cur == 4 && next == 3) 
				&& it == dp.end()){

				que.push(P(str, next));
				dp[str] = dp[s] + 1;
			}
		}
	}
}

void solve(){
	//&#21024;除空格
	a.erase(remove(a.begin(), a.end(), ' '), a.end());
	cout<<dp[a]<<endl;
}

int main(int argc, char const *argv[]){
	bfs();
	while(getline(cin, a)){
		solve();
	}
	return 0;
}