#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

short ans[1 << 24];

int adj[] = {-6, -1, 1, 6};
int pos[] = {7, 8, 9, 10, 13, 14, 15, 16};

int encode(const vector<int> &v){
	int r = 0;
	for(int i = 0; i < 8; ++i){
		r |= v[pos[i]] << (3 * i);
	}
	return r;
}

void decode(int r, vector<int> &ret){
	ret.assign(24, -1);
	for(int i = 0; i < 8; ++i){
		ret[pos[i]] = r & 7;
		r >>= 3;
	}
}

int main(){
	for(int i = 0; i < (1 << 24); ++i){
		ans[i] = -1;
	}

	vector<int> v(24, -1), w;
	for(int i = 0; i < 8; ++i){
		v[pos[i]] = i;
	}
	int c = encode(v);
	ans[c] = 0;
	queue<int> q;
	q.push(c);
	q.push(-1);
	int tm = 1;
	while(q.size() > 1){
		c = q.front();
		q.pop();
		if(c == -1){
			++tm;
			q.push(-1);
		}
		else{
			decode(c, v);
			int x;
			for(x = 7; v[x]; ++x);
			for(int i = 0; i < 4; ++i){
				int y = x + adj[i];
				if(v[y] > 0){
					swap(v[x], v[y]);
					c = encode(v);
					if(ans[c] == -1){
						ans[c] = tm;
						q.push(c);
					}
					swap(v[x], v[y]);
				}
			}
		}
	}

	while(1){
		for(int i = 0; i < 8; ++i){
			if(scanf("%d", &v[pos[i]]) == EOF){
				return 0;
			}
		}
		printf("%d\n", ans[encode(v)]);
	}
}