#include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int a[],int b[],int c,int p){
  int e[8];
  int *d;
  d=&a[b[0]*2097152+b[1]*262144+b[2]*32768+b[3]*4096+b[4]*512+b[5]*64+b[6]*8+b[7]];
  if(*d==-1||*d>c){
    *d=c;
    switch(p){
    case 0:
      memcpy(e,b,sizeof(e));
      e[p]=b[p+1];
      e[p+1]=b[p];
      f(a,e,c+1,p+1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+4];
      e[p+4]=b[p];
      f(a,e,c+1,p+4);
      break;
    case 1:
    case 2:
      memcpy(e,b,sizeof(e));
      e[p]=b[p-1];
      e[p-1]=b[p];
      f(a,e,c+1,p-1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+1];
      e[p+1]=b[p];
      f(a,e,c+1,p+1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+4];
      e[p+4]=b[p];
      f(a,e,c+1,p+4);
      break;
    case 3:
      memcpy(e,b,sizeof(e));
      e[p]=b[p-1];
      e[p-1]=b[p];
      f(a,e,c+1,p-1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+4];
      e[p+4]=b[p];
      f(a,e,c+1,p+4);
      break;
    case 4:
      memcpy(e,b,sizeof(e));
      e[p]=b[p-4];
      e[p-4]=b[p];
      f(a,e,c+1,p-4);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+1];
      e[p+1]=b[p];
      f(a,e,c+1,p+1);
      break;
    case 5:
    case 6:
      memcpy(e,b,sizeof(e));
      e[p]=b[p-1];
      e[p-1]=b[p];
      f(a,e,c+1,p-1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p+1];
      e[p+1]=b[p];
      f(a,e,c+1,p+1);
      memcpy(e,b,sizeof(e));
      e[p]=b[p-4];
      e[p-4]=b[p];
      f(a,e,c+1,p-4);
      break;
    case 7:
      memcpy(e,b,sizeof(e));
      e[p]=b[p-4];
      e[p-4]=b[p];
      f(a,e,c+1,p-4);
      memcpy(e,b,sizeof(e));
      e[p]=b[p-1];
      e[p-1]=b[p];
      f(a,e,c+1,p-1);
      break;
    }
  }
  return;
}
int main(){
  int i;
  int b[8]={0,1,2,3,4,5,6,7};
  int *a=new int[16777216];
  memset(a,-1,sizeof(int)*16777216);
  f(a,b,0,0);
  for(;;){
    b[0]=-1;
    cin>>b[0];
    if(b[0]==-1)
      break;
    for(i=1;i<8;i++)
      cin>>b[i];
    cout<<(int)a[b[0]*2097152+b[1]*262144+b[2]*32768+b[3]*4096+b[4]*512+b[5]*64+b[6]*8+b[7]]<<endl;
  }
  delete[]a;
  return 0;
}