#include <iostream>
#include <queue>
#include <set>

#define inRange(x, y, w, h) (0 <= x && x < w && 0 <= y && y < h)
#define idx_x(n, w) (n % w)
#define idx_y(n, w) (n / w)
#define idx(x, y, w) (y*w + x)

using namespace std;

struct puzzle
{
	int count;
	int prev;
	char map[8];
};

bool operator<( const puzzle &a, const puzzle &b )
{
	if(a.count < b.count) return true;
	if(a.count > b.count) return false;
	for(int i=0; i < 8; ++i) {
		if(a.map[i] < b.map[i]) return true;
		if(a.map[i]	> b.map[i]) return false;
	}
	return false;
}

bool judge( char map[8] )
{
	for(int i=0; i < 8; ++i)
		if(map[i] != i+'0') return false;
	return true;
}

int main( void )
{
	int dx[] = { 0, 0, -1, 1 };
	int dy[] = { -1, 1, 0, 0 };

	while(true) {
		queue<puzzle> q;
		set<puzzle> m;
		puzzle start;
		bool eof = false;
		for(int i=0; i < 8; ++i) {
			if(cin >> start.map[i]) { }
			else {
				eof = true; break;
			}
		}
		if(eof) break;
		if(judge(start.map)) { cout << 0 << endl; continue; }
		start.count = 0;
		for(int i=0; i < 8; ++i) if(start.map[i] == '0') { start.prev = i; break; }
		q.push(start); m.insert(start);
		int answer = 0;
		while(!q.empty() && !answer) {
			puzzle tmp, work;
			tmp = q.front(); q.pop();
			for(int i=0; i < 8 && !answer; ++i) {
				if(tmp.map[i] == '0') {
					for(int j=0; j < 4 && !answer; ++j) {
						int mx = idx_x(i, 4) + dx[j];
						int my = idx_y(i, 4) + dy[j];
						if(inRange(mx, my, 4, 2)) {
							if(tmp.prev == idx(mx, my, 4)) continue;
							work = tmp;
							work.prev = i;
							++work.count;
							swap(work.map[idx(mx, my, 4)], work.map[i]);
							if(judge(work.map)) {
								answer = work.count;
							}
							else if(!m.count(work)){
								q.push(work); m.insert(work);
							}
						}
					}
				}
			}
		}
		cout << answer << endl;
	}

	return 0;
}