#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

struct puzzle {
	int f[8], space, cnt;
	bool operator<(const puzzle&p)const {
		rep(i, 8) {
			if (f[i] == p.f[i])continue;
			return f[i] < p.f[i];
		}
		return false;
	}
};
int dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
char dir[]{ 'u','l','d','r' };
int main() {
	string s;
	while (getline(cin, s)) {
		stringstream ss(s);
		puzzle in;
		rep(i, 8) {
			ss >> in.f[i];
			if (!in.f[i])in.space = i;
		}
		queue<puzzle>que;
		map<puzzle, bool>mp;
		in.cnt = 0;
		puzzle u, v;
		que.push(in);
		mp[in] = true;
		while (!que.empty()) {
			u = que.front(); que.pop();
			bool flag = true;
			rep(i, 8) {
				if (u.f[i] != i)flag = false;
			}
			if (flag) {
				cout << u.cnt << endl;
				break;
			}
			int sx = u.space / 4;
			int sy = u.space % 4;
			rep(i, 4) {
				int nx = sx + dx[i];
				int ny = sy + dy[i];
				if (nx < 0 || ny < 0 || nx >= 2 || ny >= 4)continue;
				v = u;
				swap(v.f[u.space], v.f[nx * 4 + ny]);
				v.space = nx * 4 + ny;
				if (!mp[v]) {
					mp[v] = true;
					v.cnt++;
					que.push(v);
				}
			}
		}
	}
}