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

class State{
public:
  int p[2][4];
  int cost;
  int sx, sy;
  bool operator < (const State &s) const {
    for(int i = 0; i < 2; i++) {
      for(int j = 0; j < 4; j++) {
	if(p[i][j] != s.p[i][j]) return p[i][j] < s.p[i][j];
      }
    }
    return false;
  }
};

int main() {
  set<State> data;
  State s;
  for(int i = 0; i < 2; i++) {
    for(int j = 0; j < 4; j++) {
      s.p[i][j] = i*4+j;
    }
  }
  s.sx = 0;
  s.sy = 0;
  s.cost = 0;
  data.insert(s);
  queue<State> Q;
  Q.push(s);
  while(!Q.empty()) {
    s = Q.front();
    Q.pop();
    int d[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
    for(int i = 0; i < 4; i++) {
      State ts = s;
      ts.sx += d[i][0];
      ts.sy += d[i][1];
      ts.cost++;
      if(ts.sx < 0 || ts.sx >= 4) continue;
      if(ts.sy < 0 || ts.sy >= 2) continue;
      int temp = ts.p[ts.sy][ts.sx];
      ts.p[ts.sy][ts.sx] = ts.p[s.sy][s.sx];
      ts.p[s.sy][s.sx] = temp;
      if(data.find(ts) != data.end()) continue;
      data.insert(ts);
      Q.push(ts);
    }
  }

  State input;
  while(cin >> input.p[0][0]) {
    for(int i = 1; i < 8; i++) {
      cin >> input.p[i/4][i%4];
    }

    cout << data.find(input)->cost << endl;
  }
}