#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<cstdlib>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int dx[]={-4,-1,1,4},e[]={5040,720,120,24,6,2,1,1};

typedef struct State {
	State(int len){}
	State(){}
	int v[10];//v[8]:zero position v[9]:Fstar
	int& operator[](int i) { return v[i]; }
	bool operator==(const State& r) const { return equal(v,v+8,r.v);}
	bool operator<(const State& r) const { return lexicographical_compare(v,v+8,r.v,r.v+8);}
} vi;

typedef pair<vi,int> Node;
bool operator<(const Node &l,const Node &r){
	//[]ツづ皇onstツづづ按つ「ツづ個づ古づ猟づ按つ「ツ姪債様
	//priority_queueツづ使ツづュツづェツづゥツづ個づ逆ツ転
	return l.first.v[9]!=r.first.v[9]?
	l.first.v[9]>r.first.v[9]:
	l.second>r.second;
}

int getHash(vi s){
	//minimal perfect hash
	int r=0;
	rep(i,8){
		r+=s[i]*e[i];
		for(int j=i+1;j<8;j++)if(s[j]>s[i])s[j]--;
	}
	return r;
}

//heuristic function by Manhattan distance
int md(vi &v){
	int r=0;
	rep(i,8)if(v[i])r+=abs(v[i]-i&3)+abs(v[i]/4-i/4);
	return r;
}
bool move(vi &v,int d){
	int &z=v[8];
	if(z+dx[d]<0||z+dx[d]>7||z==3&&d==2||z==4&&d==1)return 0;
	v[9]-=md(v);
	swap(v[z],v[z+dx[d]]);z+=dx[d];
	v[9]+=md(v)+1;
	//Fstar difference todo
	return 1;
}
int main(){
	vi s(10),g(10);
	rep(i,8)g[i]=i;
	while(cin>>s[0]){
		s[8]=0;
		rep(i,7){
			cin>>s[i+1];
			if(!s[i+1])s[8]=i+1;
		}
		//A* search
		int cc,hash; vi cs,ns;
		priority_queue<Node> Q;
		int Fstar[40320];
		fill(Fstar,Fstar+40320,1<<20);
		
		s[9]=Fstar[getHash(s)]=md(s);
		Q.push(make_pair(s,0));
		
		while(!Q.empty()){
			cs=Q.top().first;
			cc=Q.top().second; Q.pop();
			
			if(cs==g){
				cout<<cc<<endl;break;
			}
			rep(d,4){
				ns=cs;
				if(!move(ns,d))continue;
				
				hash=getHash(ns);
				
				if(Fstar[hash]>ns[9])
				Q.push(make_pair(ns,cc+1)),Fstar[hash]=ns[9];
			}
		}
	}
	return 0;
}