#include<iostream>
#include<queue>
#include<vector>

using namespace std;

class Puzzle{

public:
  vector<vector<int> > p;
  int lastorder;//1 ???  3?????? 5 ??????
  int ordernum;

  Puzzle(){
    this->p.resize(2);

    for(int i=0;i<2;i++){
      this->p[i].resize(4);
    }

    this->lastorder = 60;
    this->ordernum = 0;

  }

  Puzzle& operator=(const Puzzle& p1){
    for(int i=0;i<2;i++){
      for(int j=0;j<4;j++){
        this->p[i][j] = p1.p[i][j];
      }
    }
    this->lastorder = p1.lastorder;
    this->ordernum = p1.ordernum;
  }

  ~Puzzle(){

    vector<vector<int> >().swap(p);
  }

};

int main(){

  while(!cin.eof()){

    queue<Puzzle> q;

    Puzzle Start;

    for(int i=0;i < 2; i++){
      for(int j=0;j < 4; j++){
        cin >> Start.p[i][j];
      }
    }


    q.push(Start);

    while(!q.empty()){

      Puzzle tmp;
      tmp = q.front();
      q.pop();

      bool ok = true;

      for(int i=0;i < 2; i++){
        for(int j=0;j < 4; j++){
          if(tmp.p[i][j] != (i*4 + j)){
            ok = false;
            break;
          }
        }
      }

      if(ok){
          cout << tmp.ordernum << endl;
          queue<Puzzle> empty;
          swap(q, empty);
          break;
      }

      int x,y;

      for(int i=0;i < 2; i++){
        for(int j=0;j < 4; j++){
          if(tmp.p[i][j] == 0){
            x = j;
            y = i;
          }
        }
      }

      tmp.ordernum++;

      Puzzle Beforetmp = tmp;

      //int beforeorder = tmp.lastorder;


      if(x != 3 && Beforetmp.lastorder != 3){
        swap(tmp.p[y][x],tmp.p[y][x+1]);
        tmp.lastorder = 1;
        q.push(tmp);
        tmp = Beforetmp;
      }

      if(x != 0 && Beforetmp.lastorder !=  1){
        swap(tmp.p[y][x],tmp.p[y][x-1]);
        tmp.lastorder = 3;
        q.push(tmp);
        tmp = Beforetmp;
      }

      if(Beforetmp.lastorder !=  5){
        swap(tmp.p[0][x],tmp.p[1][x]);
        tmp.lastorder = 5;
        q.push(tmp);
      }



    }

  }


    return 0;
}