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

class Point{
public:
  int piece[8];
  int zero;
  int count;

  bool is_over(){
    bool over=true;
    for(int i=0; i<8; i++)
      if(piece[i]!=i)
        over=false;
    return over;
  }
  void slide_up()
  {
    swap(piece[zero],piece[zero-4]);
    zero-=4;
  }
  void slide_down()
  {
    swap(piece[zero],piece[zero+4]);
    zero+=4;
  }
  void slide_left()
  {
    swap(piece[zero],piece[zero-1]);
    zero-=1;
  }
  void slide_right()
  {
    swap(piece[zero],piece[zero+1]);
    zero+=1;
  }
};

int main()
{
  Point puz;
  for(int i=0; i<8; i++)
    cin>>puz.piece[i];

  queue<Point> que;

  for(int i=0; i<8; i++){
      if(puz.piece[i]==0){
        puz.zero=i;
      }
  }

  int c=0;
  que.push(puz);
  puz.count=0;
  while(!que.empty()){
    puz=que.front();
    que.pop();

    if(puz.is_over()){
      cout<<puz.count<<endl;
      break;
    }

    if(puz.zero>3){
      Point next1=puz; next1.slide_up();    next1.count++; que.push(next1);
    }
    if(puz.zero<4){
      Point next2=puz; next2.slide_down();  next2.count++; que.push(next2);
    }
    if(puz.zero>0){
      Point next3=puz; next3.slide_left();  next3.count++; que.push(next3);
    }
    if(puz.zero<7){
      Point next4=puz; next4.slide_right(); next4.count++; que.push(next4);
    }
  }
  return 0;
}