#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
using namespace std;
const int n=2,m=4;
const int dir=4,dx[dir]={1,0,-1,0},dy[dir]={0,1,0,-1};
struct board{
	int a[2][4],zx,zy,dep;
};
queue<board> que;
set<board> ss;
bool operator < (board a,board b){
	int i,j;
	for (i=0;i<n;i++) for (j=0;j<m;j++)
		if (a.a[i][j]!=b.a[i][j]) return a.a[i][j]<b.a[i][j];
	return false;
}
bool read(board &a){
	int i,j;
	board ans;
	a.dep=0;
	for (i=0;i<n;i++) for (j=0;j<m;j++){
		if (scanf("%d",&a.a[i][j])!=1) return false;
		if (a.a[i][j]==0){
			a.zx=i; a.zy=j;
		}
	}
	return true;
}
bool leag(int x,int y){
	return x>=0&&x<n&&y>=0&&y<m;
}
int main(){
	board st,to,ed;
	int i,j,x,y;
	for (i=0;i<n;i++) for (j=0;j<m;j++)
		st.a[i][j]=i*m+j;
	st.zx=0; st.zy=0; st.dep=0;
	que.push(st); ss.insert(st);
	while (que.size()){
		st=que.front();
		que.pop();
		for (i=0;i<dir;i++){
			if (!leag(x=st.zx+dx[i],y=st.zy+dy[i])) continue;
			to=st; to.dep++;
			swap(to.a[to.zx][to.zy],to.a[x][y]);
			to.zx=x; to.zy=y;
			if (ss.find(to)!=ss.end()) continue;
			que.push(to); ss.insert(to);
		}
	}
	while (read(ed)){
		if (ss.find(ed)==ss.end()){
			puts("-1");
			continue;
		}
		printf("%d\n",ss.find(ed)->dep);
	}
	return 0;
}
