#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<deque>
#include<sstream>
#include<iomanip>
#include<sstream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<climits>
#include<map>
#include<set>
#include<list>
#include<cassert>
#include<deque>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define INF 1<<27
#define all(n) n.begin(),n.end()
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define F first
#define S second
#define pb push_back
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef pair<string,int> SI;
typedef long long ll;
typedef unsigned long long ull;
const int d[4] = {-2,-8,+2,+8};

string::size_type zero_finder(string s){
  string::size_type index = s.find("0");
  return index == string::npos?-1:index;
}

inline
bool range_check(string s,int dir,int pos){
  int new_pos = pos+d[dir];
  assert(new_pos%2 == 0);
  assert(pos%2 == 0);
  if(!(0<= new_pos && new_pos <= 14))return false;
  bool pis_top,npis_top;
  if(0<= pos && pos <=6)pis_top = true;
  else if(8<= pos && pos <=14)pis_top = false;
  else assert(false);

  if(0<= new_pos && new_pos <= 6)npis_top = true;
  else if(8<= new_pos && new_pos <= 14)npis_top = false;
  else assert(false);

  if(pis_top != npis_top && (dir == 0 || dir == 2))return false;
  return true;
}

inline
void s_swap(string &s,int a,int b){
  char store = s[a];
  s[a] = s[b];
  s[b] = store;
}

int main(){
  string input;
  map<string,int> ans;

  ans["0 1 2 3 4 5 6 7"] = 0;

  queue<SI> que;

  que.push(SI("0 1 2 3 4 5 6 7",0));

  bool first_time = true;

  while(!que.empty()){
    SI si = que.front(); que.pop();
    if(ans[si.F] != 0 && !first_time)continue;

    ans[si.F] = si.S;
    first_time = false;    
    rep(i,4)
      {
	
	int pos = zero_finder(si.F);
	int n_pos = pos+d[i];

	assert(pos != -1);

	if(!range_check(si.F,i,pos))continue;

	string next_s = si.F;
	s_swap(next_s,pos,n_pos);    
	que.push(SI(next_s,si.S+1));
      }
  }
  
 while(getline(cin,input))
  {
    cout << ans[input] << endl;
  }
  return 0;
}