#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define size_of(a) (int)(a).size()
#define allof(a) (a).begin(),(a).end()

int to[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},
};

struct State {
	int step, p[8];
	
	bool isEnd() {
		for_(i,0,8) if (p[i] != i) return false;
		return true;
	}
	
	int zero() {
		for_(i,0,8) if (p[i] == 0) return i;
		return -1;
	}
	
	int getHash() {
		int hash = 0;
		for_(i,0,8) hash = 10 * hash + p[i];
		return hash;
	}
};

int main() {
	State ini;
	ini.step = 0;
	
	int dumy;
	while (cin >> dumy) {
		ini.p[0] = dumy;
		for_(i,1,8) cin >> ini.p[i];
		
		set< int > hash;
		
		queue< State > que;
		que.push(ini);
		
		hash.insert(ini.getHash());
		
		while (!que.empty()) {
			State s = que.front(); que.pop();
			
			if (s.isEnd()) {
				cout << s.step << endl;
				break;
			}
			
			++s.step;
			int t = s.zero();
			
			for_(j,0,3) {
				int nxt = to[t][j];
				
				if (nxt != -1) {
					swap(s.p[t], s.p[nxt]);
					
					int hs = s.getHash();
					
					if (hash.find(hs) == hash.end()) {
						que.push(s);
						hash.insert(hs);
					}
					
					swap(s.p[t], s.p[nxt]);
				}
			}
		}
	}
}