#include <iostream>
#include <set>
#include <queue>
#define SWAP(A,B,C) ((C)=(A),(A)=(B),(B)=(C))
using namespace std;
class State{
public:
  int p[2][4];
  int cost;
  int zx, zy;
  bool operator < (const State &s) const {
    for(int i = 0; i < 2; i++) {
      for(int j = 0; j < 4; j++) {
    if(p[i][j] != s.p[i][j]) return p[i][j] < s.p[i][j];
      }
    }
    return false;
  }
};

int dx[] = {0,1,0,-1},dy[] = {-1,0,1,0};
int main()
{
	State a;
	for(int i = 0;i<2;i++)
		for(int j = 0;j<4;j++)
			a.p[i][j] = i*4+j;
	a.cost = 0;a.zx=0;a.zy=0;
	set<State> data;data.insert(a);
	queue<State> q;q.push(a);
	
	while(!q.empty())
	{
		State b = q.front();q.pop();	
		for(int i = 0;i<4;i++)
		{
			State tb = b;
			int nx = tb.zx + dx[i];
			int ny = tb.zy + dy[i];
			if(nx<0||nx>=4||ny<0||ny>=2)continue;
			tb.cost++;
			int temp;
			SWAP(tb.p[b.zy][b.zx],tb.p[ny][nx],temp);
			tb.zy = ny;tb.zx = nx;
			if(data.find(tb)!=data.end())continue;
			data.insert(tb);
			q.push(tb);
		}
	}
	State input;
	while(cin >> input.p[0][0]) {
    	for(int i = 1; i < 8; i++) {
      	cin >> input.p[i/4][i%4];
    	}
		cout << data.find(input)->cost << endl;
	}
	
}