#include <iostream>
#include <cstdio>//
#include <cmath>
#include <cstring> // 主要有str??和 memset 
#include <cstdlib> //rand()  atoi
#include <ctime> //??不住怎?? (double)clock() / CLOCKS_PER_SEC 
#include <cctype>//tolower也是?里的 
#include <stack> 
#include <vector>
#include <algorithm> 
#include <set>//??就没用?set 
#include <string>
#include <sstream>
#include <map>
#include <queue>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int inf = 0x3f3f3f3f;
#define all(x) x.begin(),x.end() 
#define ins(x) inserter(x, x.begin())
//key0:不能理解?是干嘛的，照抄?佳汝的


int mp[2][4];
int m, n, dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

bool check(int x, int y){
	if (x < 0 || x > 1) return false;
	if (y < 0 || y > 3) return false;
	return true;
}
struct state{
	int l, x, y;
	int m[2][4];
};
int compute(int m[2][4]){
	int *p = m[0] + 0;
//	printf("%dhaha\n", *(m[1] + 3));
	int sum = 0;
	for (int i = 0; i < 8; ++i){
		sum *= 10;
//		printf("%d ", *(p-i));
		sum += *(p+i);
	}
//	printf("\n%d\n", sum);
	return sum;
}
queue<state> q;
set<int> vis;
int bfs(){
	while(q.size()) q.pop();
	int i, j;
	state temp;
	vis.clear();
	for (i = 0; i < 2; ++i){
		for (j = 0; j < 4; ++j){
			if (mp[i][j] == 0){
				temp.x = i;
				temp.y = j;
				break;
			}
		}
	}
	
	memcpy(temp.m, mp, sizeof mp);
	if (compute(temp.m) == 1234567) return 0;
//	printf("haha\n");
	vis.insert(compute(temp.m));
	temp.l = 0;
	q.push(temp);
	while(q.size()){
		temp = q.front(); q.pop();
	//	printf("%d\n", compute(temp.m));
		for (i = 0; i < 4; ++i){
			int nx = temp.x + dir[i][0];
			int ny = temp.y + dir[i][1];
			if (!check(nx, ny)) continue;
		//	printf("%d %d\n", nx, ny);
		//	getchar(); getchar();
			state newtemp;
			memcpy(newtemp.m, temp.m, sizeof temp.m);
			swap(newtemp.m[nx][ny], newtemp.m[temp.x][temp.y]);
			int num = compute(newtemp.m);
			if (num == 1234567){
				return temp.l + 1;
			}
			else if(vis.count(num)) continue;
		//	printf("%dere  %d\n", num, temp.l+1);
		//	getchar(); getchar();
			vis.insert(num);
			newtemp.l = temp.l + 1;
			newtemp.x = nx; newtemp.y = ny;
			q.push(newtemp);
		}
	}
} 
int main(){
//  ios::sync_with_stdio(false);
#ifdef LOCAL
//	freopen("output.txt", "w", stdout);
//    freopen("input.txt", "r", stdin);
#endif
	int i, j, kase;
//	printf("%d\n", sum);
	while(scanf("%d", &mp[0][0]) == 1){
		int *p = mp[0] + 1;
		for (i = 0; i < 7; ++i){
			scanf("%d", (p+i));
		}
//		for (i = 0; i < 2; ++i){
//			for (j = 0; j < 4; ++j) printf("%d ", mp[i][j]);
//		}
//		puts("");
		printf("%d\n", bfs());
	}
	return 0;
}