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

using namespace std;

map<string, int> dp;
int MOVE[4] = {-1, 1, -4, 4};

bool check(int x){ return 0 <= x && x < 8; }

void bfs(){
	string s = "01234567";
	dp[s] = 0;
	queue<string> q;
	q.push(s);
	while(!q.empty()){
		string now = q.front();
		q.pop();
		int z = 0;
		for(int i = 1; i < 8; ++i){
			if(now[i] == '0'){
				z = i;
				break;
			}
		}
		
		for(int i = 0; i < 4; ++i){
			string next = now;
			int n = z + MOVE[i];
			if(check(n) && !(i == 0 && z == 4) && !(i == 1 && z == 3)){
				swap(next[n], next[z]);
				if(dp.count(next) == 0){
					q.push(next);
					dp[next] = dp[now] + 1;
				}
			}
		}
	}
}

int main(){
	bfs();
	string line;
	while(getline(cin, line)){
		line.erase(remove(line.begin(), line.end(), ' '), line.end());
		cout << dp[line] << endl;
	}
}
