/*
 * hujx.cpp
 *
 *  Created on: 2015年5月21日
 *      Author: dell
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
#include<set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
//#pragma comment(linker,"/STACK:1024000000,1024000000")
int n,m;
#define M 110
#define N 500010
#define Mod 258280327
#define p(x,y) make_pair(x,y)
int fac[12] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
struct Node{
	int x,y;
	int mp[2][5];
	int step;
};
int vis[1000010];
int getHash(int s[][5]){
	int tmp[10];
	int k=0;
	for(int i=0;i<2;i++)
		for(int j=0;j<4;j++)
			tmp[k++] = s[i][j];
	int res=0;
	for(int i=0;i<8;i++){
		int c=0;
		for(int j=i+1;j<8;j++){
			if(tmp[i]>tmp[j]) c++;
		}
		res += c*fac[8-i];
	}
	return res;
}
bool check(int x,int y){
	if(x<0 || y<0 || x>=2 || y>=4)
		return false;
	return true;
}
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int mp[2][5];
int stx,sty;
int bfs(){
	queue<Node> q;
	memset(vis,0,sizeof vis);
	int hash = getHash(mp);
	if(hash == 0) return 0;
	vis[hash] = 1;
	Node cur,next;
	cur.x = stx;
	cur.y = sty;
	cur.step = 0;
	memcpy(cur.mp,mp,sizeof mp);
	q.push(cur);
	while(!q.empty()){
		cur = q.front();
		q.pop();
		for(int i=0;i<4;i++){
			next = cur;
			next.x += dir[i][0];
			next.y += dir[i][1];
			next.step++;
			if(!check(next.x,next.y)) continue;
			swap(next.mp[next.x][next.y],next.mp[cur.x][cur.y]);
			hash = getHash(next.mp);
			if(hash == 0)
				return next.step;
			if(vis[hash]) continue;
			vis[hash] = 1;
			q.push(next);
		}
	}
	return 0;
}
int main(){
	while(sf(mp[0][0])!=EOF){
		for(int i=1;i<4;i++)
			sf(mp[0][i]);
		for(int i=0;i<4;i++)
			sf(mp[1][i]);
		for(int i=0;i<2;i++)
			for(int j=0;j<4;j++){
				if(mp[i][j] == 0){
					stx = i;
					sty = j;
				}
			}
		int ans = bfs();
		printf("%d\n",ans);
	}
    return 0;
}