#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;
typedef vector<int> vi;
typedef pair<pair<int,int>,vi> Node;//((CurFstar,CurCost),CurState)
int dx[]={-4,-1,1,4};

//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;
	swap(v[z],v[z+dx[d]]);
	v[8]+=dx[d];
	return 1;
}
int main(){
	vi s(9),g(9);
	rep(i,9)g[i]=i%8;
	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 cFstar,nFstar,cc; vi cs,ns;
		priority_queue<Node> Q; map<vi,int> Fstar;
		Q.push(make_pair(make_pair(-md(s),0),s)); Fstar[s]=md(s);
		
		while(!Q.empty()){
			cs=Q.top().second;
			cFstar=-Q.top().first.first;
			cc=Q.top().first.second;
			Q.pop();
			if(cs==g){
				cout<<-cc<<endl;break;
			}
			rep(d,4){
				ns=cs;
				nFstar=cFstar-md(cs)+md(ns)+1;
				if(!move(ns,d))continue;
				if(Fstar.find(ns)==Fstar.end()||Fstar[ns]>nFstar)
				Q.push(make_pair(make_pair(-nFstar,cc-1),ns)),Fstar[ns]=nFstar;
			}
		}
	}
	return 0;
}