#include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define mk make_pair
#define f first
#define s second
using namespace std;
bool t[8][8][8][8][8][8][8][8];
int dx[]={1,-1,4,-4};
void plu(string x){
  t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]]=1;
}
bool pl(string x){
  return t[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]][x[7]];
}
bool ch(){
  return t[0][1][2][3][4][5][6][7];

}
int main(){
  int ww[8],y;
  while(cin>>ww[0]){
    memset(t,0,sizeof(t));
    string v,a,x;
    r(i,7)cin>>ww[i+1];
    r(i,8)v+=ww[i];
    queue<pair<string,int> >q;
    plu(v);
    if(ch()){cout<<0<<endl;goto L;}
    q.push(mk(v,0));
    while(!q.empty()){
      pair<string,int>p=q.front();q.pop();
      x=p.f;
      r(i,8)if(!x[i])r(j,4){
        if(j==0&&i==3)continue;
        if(j==1&&i==4)continue;
        y=i+dx[j];
        if(y>=0&&y<8){
        a=x;
        swap(a[i],a[y]);
        if(!pl(a)){
          q.push(mk(a,p.s+1));
          plu(a);
          if(ch()){cout<<p.s+1<<endl;goto L;}
        }
        }
      }
    }
    L:;
  }
}