#include <iostream>
#include <queue>
#include <set>
using namespace std;

struct P{
  int n, org;
  int state[8];
};

P copy(P p){
  P q;
  q.n = p.n;
  q.org = p.org;
  for( int i=0;i<8;i++ )
    q.state[i] = p.state[i];
  return q;
}

int toN(P &p){
  int r=0;
  for( int i=7,b=1;i>=0;i--, b*=10 )
    r += p.state[i] * b;
  return r;
}

int main(){
  int state[8];
  P p, p2;

  while( cin >> p.state[0] ){
    for( int i=1;i<8;i++ )
      cin >> p.state[i];

    p.n=0; p.org=-1;
    queue<P> q;
    q.push(p);
    set<int> st;
    while( q.size() ){
      p = q.front(); q.pop();

      int num = toN(p);
      /*
      cout << "cont : " << num << " " << p.org << endl
	   << " ";
      for( int i=0;i<8;i++ )cout << p.state[i];
      cout << endl;
      */
      if( st.size() && st.find(num) != st.end() ){
	continue;
      }

      st.insert(toN(p));

      bool flag = false;
      for( int i=0;i<8&&p.state[i]==i;i++ )
	if( i==7 ) flag=true;

      if( flag ){
	cout << p.n << endl;
	break;
      }


      int pos=-1;
      for( int i=0;i<8;i++ ){
	if( p.state[i] == 0 ){
	  pos = i; break;
	}
      }

      p.n++;
      p.org = pos;
      if( pos<4 ){ // top
	p2=copy(p);
	swap( p2.state[pos], p2.state[pos+4] );
	q.push(p2);
      }else{
	p2=copy(p);
	swap( p2.state[pos], p2.state[pos-4] );
	q.push(p2);
      }
      if( pos % 4 !=  0 ){ // not left
	p2=copy(p);
	swap( p2.state[pos], p2.state[pos-1] );
	q.push(p2);
      }
      if( pos %4 != 3 ){ // not right
	p2=copy(p);
	swap( p2.state[pos], p2.state[pos+1] );
	q.push(p2);
      }
    }
  }

  return 0;
}