#include<complex>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<deque>
#include<queue>
#include<climits>
#include<list>
#include<stack>
#include<ctime>
#define all(n) n.begin(),n.end()
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define insert(a,b,c) PP(SI(a,b),c)
#define F first
#define S second
#define pb push_back
#define pf push_front
#define INF (1<<27)
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)

using namespace std;
typedef pair<int,int> P;
typedef pair<string,int> SI;
typedef pair<SI,int> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
const string solve = "0 1 2 3 4 5 6 7";
const int mv[4] = {+2,-2,+8,-8};

int main(){
  string s;
  while(getline(cin,s)){
    //cout <<"s = " << s[14] << endl;
    //rep(i,4){
    //int next = 14+mv[i];
    //if(!(0<=next && next<=14))continue;
    //if(0<=next-mv[i] && next-mv[i] <= 6){
    //	if((i == 0 || i == 1) && !(0<=next && next<=6))continue;
    //}
    //if(8<= next-mv[i] && next-mv[i] <=14){
    //	if((i == 0 || i == 1) && !(8<=next && next<=14))continue;
    //}
    //cout << s[next] << " i = " << i << ",next = " << next<< endl;
    //}    
    //cout << "iiiii" << endl;

    //rep(i,8)cout << i+1 << " ";
    //cout << endl;
    //rep(i,8)cout << s[i*2] << " ";
    //cout << endl;
    int zero = -1;
    rep(i,s.length())
      if(s[i] == '0'){
	zero = i;
	break;
      }
    assert(zero != -1);
    map<string,bool> used_pattern;
    deque<PP> deq;
    deq.pf(insert(s,0,zero));// string / counter / 0-pos
    used_pattern[s] = true;
    int cnt = INT_MAX;
    
    while(!deq.empty()){
      PP pp = deq.front(); deq.pop_front();
      //cout << "pp.F,F = " << pp.F.F << endl;
      // cout << "pp = "<< pp.F.F << "," << pp.F.S << "," << pp.S  << endl; 
      if(pp.F.S >= cnt)continue;
      if(pp.F.F == solve){
	cnt = min(cnt,pp.F.S);
	continue;
      }

      rep(i,4){
	int next = pp.S + mv[i];
	if(!(0<= next && next <= 14))continue;
	if(0<=pp.S && pp.S<=6){
	  if((i == 0 || i == 1) && !(0<=next && next<=6))continue;
	}
	else if(8<=pp.S && pp.S <=14){
	  if((i == 0 || i == 1) && !(8<= next && next<=14))continue;
	}
	string bkup = pp.F.F;
	
	//cout <<"o? " << pp.F.F[pp.S] << ", F["<<next<<"] = " << pp.F.F[next]  << ", pre pp.F.F = " << pp.F.F<< endl;
	assert(pp.F.F[pp.S] == '0');	
	char pre = pp.F.F[next];
	pp.F.F[next] = pp.F.F[pp.S];
	pp.F.F[pp.S] = pre;
	if(used_pattern[pp.F.F]){
	  pp.F.F = bkup;
	  continue;
	}
	used_pattern[pp.F.F] = true;
	//cout << "change pp.F.F = " << pp.F.F <<",next = " << next<< endl;
	deq.pf(insert(pp.F.F,pp.F.S+1,next));
	pp.F.F = bkup;
      }

    }
    assert(cnt != INT_MAX);
    cout << cnt << endl;
  }
return 0;
}



/*
Way to check the time of processing speed

clock_t start,end;
start = clock();

------function-------

end = clock();
cout << setiosflags(ios::fixed) << setprecision(10) << (double)(end-start)/CLOCKS_PER_SEC << endl;


*/