#include<iostream>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;
int main(){
  vector<vector<int> >pos(8);
  pos[0].pb(1);pos[0].pb(4);
  pos[1].pb(-1);pos[1].pb(1);pos[1].pb(4);
  pos[2].pb(-1);pos[2].pb(1);pos[2].pb(4);
  pos[3].pb(-1);pos[3].pb(4);
  pos[4].pb(-4);pos[4].pb(1);
  pos[5].pb(-4);pos[5].pb(1);pos[5].pb(-1);
  pos[6].pb(-4);pos[6].pb(1);pos[6].pb(-1);
  pos[7].pb(-4);pos[7].pb(-1);
  string ans="01234567";
  char cc;
  while(cin>>cc){
    int zero;
    string fir="";
    fir+=cc;
    if(cc=='0')
      zero=0;
    for(int i=0;i<7;i++){
      char c;
      cin>>c;
      fir+=c;
      if(c=='0')
	zero=i+1;
    }
    queue<pair<pair<int,int>,string> >que;
    que.push(mp(mp(0,zero),fir));
    if(ans.compare(fir)==0){
      cout<<0<<endl;
    }else{
      bool dp[8][8][8][8][8][8][8];
      memset(dp,0,sizeof(dp));
      dp[fir[0]-'0'][fir[1]-'0'][fir[2]-'0'][fir[3]-'0'][fir[4]-'0'][fir[5]-'0'][fir[6]-'0']=fir[7]-'0';
      while(!(que.empty())){
	pair<pair<int,int>,string> now=que.front();
	que.pop();
	if(ans.compare(now.s)==0){
	  printf("%d\n",now.f.f);
	  break;
	}
	for(int i=0;i<pos[now.f.s].size();i++){
	  int flag=1;
	  string str=now.s;
	  char tmp=str[now.f.s];
	  str[now.f.s]=str[now.f.s+pos[now.f.s][i]];
	  str[now.f.s+pos[now.f.s][i]]=tmp;
	  if(dp[str[0]-'0'][str[1]-'0'][str[2]-'0'][str[3]-'0'][str[4]-'0'][str[5]-'0'][str[6]-'0']==false){
	    que.push(mp(mp((now.f.f)+1,now.f.s+pos[now.f.s][i]),str));
	    dp[str[0]-'0'][str[1]-'0'][str[2]-'0'][str[3]-'0'][str[4]-'0'][str[5]-'0'][str[6]-'0']=true;
	  }
	}
      }
    }
  }
  return 0;
}