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

long swap(long x,int i,int j){
  long y = 0;
  for(int n=0; n<8; n++){
    if(n==i){
      y += ((x>>(j*4))&15)<<(4*n);
    } else if(n==j){
      y += ((x>>(i*4))&15)<<(4*n);
    } else {
      y += ((x>>(n*4))&15)<<(4*n);
    }
  }
  return y;
}

void pr(long x){
  for(int i=0; i<8; i++) cout << ((x>>(4*i))&15) << " ";
  cout << endl;
}

int main(){
  string line;
  int a[8];
  long finish = 0;
  for(int i=0; i<8; i++) finish += i<<(i*4);
  pr(finish);
  while(getline(cin,line)){
    istringstream s(line);
    for(int i=0; i<8; i++) s >> a[i];
    set<long> done;
    queue<long> que;
    long x = 0;
    for(int i=0; i<8; i++) x += a[i] << (i*4);
    done.insert(x);
    que.push(x);
    int n = 0;
    pr(x);
    if(x == finish){
      cout << 0 << endl;
      continue;
    }

    while(!que.empty()){
      queue<long long> next;
      n++;
      while(!que.empty()){
        long y = que.front();
        que.pop();
        //pr(y);
        for(int i=0; i<8; i++){
          if(((y>>(4*i))&15)==0){
            if(i==0){
              next.push(swap(y,0,1));
              next.push(swap(y,0,4));
            }else if(i==1){
              next.push(swap(y,1,0));
              next.push(swap(y,1,2));
              next.push(swap(y,1,5));
            }else if(i==2){
              next.push(swap(y,2,1));
              next.push(swap(y,2,3));
              next.push(swap(y,2,6));
            } else if(i==3){
              next.push(swap(y,3,2));
              next.push(swap(y,3,7));
            } else if(i==4){
              next.push(swap(y,4,0));
              next.push(swap(y,4,5));
            } else if(i==5){
              next.push(swap(y,5,4));
              next.push(swap(y,5,6));
              next.push(swap(y,5,1));
            } else if(i==6){
              next.push(swap(y,6,5));
              next.push(swap(y,6,7));
              next.push(swap(y,6,2));
            } else if(i==7){
              next.push(swap(y,7,6));
              next.push(swap(y,7,3));
            }
          }
        }
      }
      que = queue<long>();
      while(!next.empty()){
        long y = next.front();
        next.pop();
        if(done.find(y) == done.end()){
          que.push(y);
          done.insert(y);
        }
      }
      if(done.find(finish) != done.end()){
        break;
      }
    }
    cout << n << endl;
  }
  return 0;
}