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

char str[8];
char fin[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
int dir[4] = { -1, 1, -4, 4 };

struct point{
	char temp[8];
	int num;
	point(char* tt, int nn){
		strcpy(temp, tt);
		num = nn;
	}
};

bool equal(char* p1, char* p2){
	for (int i = 0; i<8; i++){
		if (p1[i] != p2[i]) return false;
	}
	return true;
}

int bfs(char* s){
	queue<point> q;
	q.push(point(s, 0));

	while (q.size()){
		point cur = q.front(); q.pop();
		if (equal(cur.temp, fin)) return cur.num;
		int zx;
		for (int i = 0; i<8; i++){ if (cur.temp[i] == '0') { zx = i; break; } }
		for (int i = 0; i<4; i++){
			int nx = zx + dir[i];
			if (nx >= 0 && nx<8){
				char t[8];
				strcpy(t, cur.temp);
				char a = t[zx]; t[zx] = t[nx]; t[nx] = a;
				q.push(point(t, cur.num + 1));
			}
		}
	}
}

int main(){
	while (cin >> str){
		cout << bfs(str) << endl;
	}
}
