#include<iostream>
#include<queue>

using namespace std;

class Puzzle{

public:
  int p[2][4];
  int lastorder;//1 ??? 2 ??? 3?????? 4 ???
  int ordernum;

};

int main(){

  while(1){

    queue<Puzzle> q;

    Puzzle Start;

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

    Start.lastorder = 60;
    Start.ordernum = 0;

    q.push(Start);

    while(1){

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

      bool ok = true;

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

      if(ok){
          cout << tmp.ordernum << endl;
          break;
      }

      int x,y;

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

      int beforeorder = tmp.lastorder;
      tmp.ordernum++;

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

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

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

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

    }



  }


    return 0;
}