#include <iostream>
#include <string>
#include <map>
#include <sstream>
#include <algorithm>
#include <deque>

using namespace std;

class State{
public:
  string str;
  int posz;
  int times;
};

int main(void){
  string str;

  while(getline(cin,str)){
	string tmps="";
	istringstream iss(str);
	char tmp;
	map<string, bool> visited;
	deque<State> q;
	State s,t;
	int i=0;

	while(iss >> tmp){
	  tmps += tmp;
	  if(tmp == '0'){
		s.posz = i;
	  }
	  i++;
	}

	s.str = tmps;
	s.times = 0;
	q.push_back(s);

	while(!q.empty()){
	  s = q.front(); q.pop_front();
	  //cout << s.str << " " << s.posz << endl;
	  if(s.str == "01234567"){
		cout << s.times << endl;
		break;
	  }

	  if(visited[s.str] == true){
		//cout << " passed!" << endl;
		continue;
	  }
	  visited[s.str] = true;

	  t = s;
	  if(t.posz == 0){
		swap(t.str[t.posz], t.str[t.posz+1]);
		if(visited[t.str] == false){
		  t.posz += 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz+4]);
		if(visited[t.str] == false){
		  t.posz += 4;
		  t.times++;
		  q.push_back(t);
		}
	  }else if(t.posz == 4){
		swap(t.str[t.posz], t.str[t.posz+1]);
		if(visited[t.str] == false){
		  t.posz += 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz-4]);
		if(visited[t.str] == false){
		  t.posz -= 4;
		  t.times++;
		  q.push_back(t);
		}
	  }else if(t.posz == 3){
		swap(t.str[t.posz], t.str[t.posz-1]);
		if(visited[t.str] == false){
		  t.posz -= 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz+4]);
		if(visited[t.str] == false){
		  t.posz += 4;
		  t.times++;
		  q.push_back(t);
		}
	  }else if(t.posz == 7){
		swap(t.str[t.posz], t.str[t.posz-1]);
		if(visited[t.str] == false){
		  t.posz -= 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz-4]);
		if(visited[t.str] == false){
		  t.posz -= 4;
		  t.times++;
		  q.push_back(t);
		}
	  }else if(t.posz == 1 || t.posz == 2){
		swap(t.str[t.posz], t.str[t.posz+1]);
		if(visited[t.str] == false){
		  t.posz += 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz-1]);
		if(visited[t.str] == false){
		  t.posz -= 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz+4]);
		if(visited[t.str] == false){
		  t.posz += 4;
		  t.times++;
		  q.push_back(t);
		}
	  }else{
		swap(t.str[t.posz], t.str[t.posz+1]);
		if(visited[t.str] == false){
		  t.posz += 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz-1]);
		if(visited[t.str] == false){
		  t.posz -= 1;
		  t.times++;
		  q.push_back(t);
		}
		t = s;
		swap(t.str[t.posz], t.str[t.posz-4]);
		if(visited[t.str] == false){
		  t.posz -= 4;
		  t.times++;
		  q.push_back(t);
		}
	  }
	}
  }
}