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

using namespace std;

class Puzzle{

public:
  short** p;
  short lastorder;//1 ???  3?????? 5 ??????
  short ordernum;

  Puzzle(){
    p = new short*[2];
    for(int i=0; i<2; i++){
      p[i] = new short[4];
    }
    this->lastorder = 60;
    this->ordernum = 0;
  }

  Puzzle(const Puzzle* puzzle){
    p = new short*[2];
    for(int i=0; i<2; i++){
      p[i] = new short[4];
    }
    for(int i=0;i<2;i++){
      for(int j=0;j<4;j++){
        p[i][j] = puzzle->p[i][j];
      }
    }
    this->lastorder = puzzle->lastorder;
    this->ordernum = puzzle->ordernum;
  }

  ~Puzzle(){
    for(int i=0; i<2; i++){
      delete[] p[i];
    }
    delete[] p;
  }

};


int main(){

  while(1){

    queue<Puzzle*> q;

    Puzzle* Start;
    Start = new Puzzle;

    for(short i=0;i < 2; i++){
      for(short j=0;j < 4; j++){
        cin >> Start->p[i][j];
      }
    }
    if(cin.eof()){
      break;
    }

    q.push(Start);

    while(!q.empty()){

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

      bool ok = false;

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

      if(tmp->p[0][0] == 0 && tmp->p[0][1] == 1 && tmp->p[0][2] == 2 && tmp->p[0][3] == 3 && tmp->p[1][0] == 4 && tmp->p[1][1] == 5 && tmp->p[1][2] == 6)
        ok = true;

      if(ok){
          cout << tmp->ordernum << endl;
          delete tmp;
          while(!q.empty()){
            tmp = q.front();
            q.pop();
            delete tmp;        
          }
          break;
      }

      short x,y;

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

      tmp->ordernum++;

      short Beforeorder = tmp->lastorder;

      Puzzle* puzzle;
      puzzle = new Puzzle(tmp);


      if(x != 3 && Beforeorder != 3 ){
        swap(puzzle->p[y][x],puzzle->p[y][x+1]);
        puzzle->lastorder = 1;
        q.push(puzzle);
        puzzle = new Puzzle(tmp);
      }

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

      if(Beforeorder !=  5){
        swap(puzzle->p[0][x],puzzle->p[1][x]);
        puzzle->lastorder = 5;
        q.push(puzzle);
      }

      delete tmp;

    }

  }


    return 0;
}