#include <iostream>
#include <string>
#include <queue>
#include <map>

using namespace std;

struct St{
  string str;
  int cnt;
};

int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

string getStr(char a[2][4])
{
  string str = "";
  for(int i = 0; i < 2; i++){
    for(int j = 0; j < 4; j++){
      str += a[i][j];
    }
  }

  return str;
}

int func(string str)
{
  int r = 0;
  St st1, st2;
  queue<St> que;
  map<string, bool> mpp;
  mpp[str] = true;
  st1.str = str;
  st1.cnt = 0;
  que.push(st1);

  while(!que.empty()){
    st1 = que.front();
    que.pop();
    if(st1.str == "01234567"){
      r = st1.cnt;
      break;
    }
    char a[2][4];
    string s = st1.str;
    int p, q;
    for(int i = 0; i < 2; i++){
      for(int j = 0; j < 4; j++){
	a[i][j] = s[i*4+j];
	if(a[i][j] == '0'){
	  p = i;
	  q = j;
	}
      }
    }

    for(int i = 0; i < 4; i++){
      int xdir = q+dx[i], ydir = p+dy[i];
      if(0 <= xdir && xdir < 4 && 0 <= ydir && ydir < 2){
	char w = a[p][q];
	a[p][q] = a[ydir][xdir];
	a[ydir][xdir] = w;
	string s2 = getStr(a);
	w = a[p][q];
	a[p][q] = a[ydir][xdir];
	a[ydir][xdir] = w;

	if(!mpp[s2]){
	  mpp[s2] = true;
	  st2.str = s2;
	  st2.cnt = st1.cnt+1;
	  que.push(st2);
	}
      }
    }
  }


  return r;
}

int main()
{
  char t;

  while(cin>>t){
    string s = "";
    s += t;
    for(int i = 1; i < 8; i++){
      cin >> t;
      s += t;
    }

    cout << func(s) << endl;
  }

  return 0;
}