#include <bits/stdc++.h>
using namespace std;
#define REP(i,N) for(i=0;i<N;i++)
typedef long long ll;
typedef pair<string,int> P;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
map<string, int>table;


bool mobility(int x, int y){
  return x>=0 && x<4 && y>=0 && y<2;
}

int bfs(string goal){
  int i;
  queue<P> que;
  que.push(P(goal,0));
  table[goal] = 0;
  
  while(!que.empty()){
    string now = que.front().first;
    int c = que.front().second;
    que.pop();

    int pos = now.find("0");
    int x = pos%4, y = pos/4;
    REP(i,4){
      int nx = x+dx[i], ny = y+dy[i];
      if(mobility(nx,ny)){
        string next = now;
        swap(next[pos],next[ny*4+nx]);
        if(table.count(next)==0){
          que.push(P(next,c+1));
          table[next] = c+1;
        }
      }
    }
  }
}

int main(void){
  string str;
  string goal = "01234567";
  bfs(goal);
  while(getline(cin,str)){
    int i;
    string input="";
    REP(i,str.length()){
      if(str[i] == ' ') continue;
      input+=str[i];
    }
    cout << table[input] << endl;
  }
  return 0;
}

