//AOJ 0121

//include
#include<bits/stdc++.h>

//using
using namespace std;


//define static const typedef
#define FOR(i, s, n) for(int (i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

//global
static vector<short> clear(8);
static vector<vector<short> > move(8);
static vector<short> now(8); 
static int b;
static queue<pair<pair<vector<short>, int>, int> > q;
static map<vector<short>, int> ans;
static map<vector<short>, int> cp;
//function
static int main(){
  REP(i, 8){
    clear[i] = i;
  }
  move[0].PB(1);
  move[0].PB(4);
  move[1].PB(0);
  move[1].PB(2);
  move[1].PB(5);
  move[2].PB(1);
  move[2].PB(3);
  move[2].PB(6);
  move[3].PB(2);
  move[3].PB(7);
  move[4].PB(0);
  move[4].PB(5);
  move[5].PB(1);
  move[5].PB(4);
  move[5].PB(6);
  move[6].PB(2);
  move[6].PB(5);
  move[6].PB(7);
  move[7].PB(3);
  move[7].PB(6);
  while(scanf("%d %d %d %d %d %d %d %d", &now[0], &now[1], &now[2], & now[3], &now[4], &now[5], &now[6], &now[7]) != EOF){
    REP(i, 8){
      if(!now[i]){
	b = i;
	break;
      }
    }
    if(now == clear){
      printf("0\n");
      continue;
    }
    static int d;
      ans = cp;
      ans[clear] = INF;
      q.push(pair<pair<vector<short>, int>, int>(pair<vector<short>, int>(now, 0),b));
    while(!q.empty()){
      now = q.front().F.F;
      int a = q.front().F.S;
      b = q.front().S;
      q.pop();
      if(!ans[now] || (ans[now] > a && ans[clear] > a)){
	ans[now] = a;
      }
      else if(ans[clear] == a){
	break;
      }
      else{
	continue;
      }
      //cout << "a = " << a << " b = " << b << "\n";
      REP(i, move[b].size()){
	int tmp; 
	tmp = move[b][i];
	
	swap(now[tmp], now[b]);
	q.push(pair<pair<vector<short>, int>, int>(pair<vector<short>, int>(now, a + 1), tmp));
	swap(now[tmp], now[b]);
      }
    }
    d = ans[clear];
    
    
    printf("%d\n", d);
  }
  return 0;
}