#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
#define pi acos(-1)


#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define N 80000000

map<int,int>vis;

struct stud{
  int a[10];
  int time;

  friend bool operator < (stud a,stud b)
  {
  	 return a.time>b.time;
  }
};

int hello;

int op[10]={10000000,1000000,100000,10000,1000,100,10,1};

priority_queue<stud>q;

inline int fdd(stud cur)
{
   int ans=0,i;
   fre(i,0,8)
     ans+=cur.a[i]*op[i];
   return ans;
}

inline void change(stud &aa,stud &b)
{
	 int i;
     fre(i,0,8)
      b.a[i]=aa.a[i];
	b.time=aa.time+1;
}

int bfs(stud x)
{
	int i,j,t=0;;
	while(!q.empty()) q.pop();

	x.time=0;
	q.push(x);

	stud cur;
    vis.clear();

	while(!q.empty())
	{
	    cur=q.top();
	    q.pop();

	    int temp=fdd(cur);

	    if(temp==hello)
		{
			return cur.time;
		}

	    vis[temp]++;

	    struct stud next;

        fre(i,0,8)
          if(cur.a[i]==0) break;

		if((i>0&&i<=3)||i>4)
		{
		    struct stud nnext;
		    change(cur,nnext);

		    swap(nnext.a[i],nnext.a[i-1]);

		    int temp=fdd(nnext);
		  if(!vis[temp])
			{
				q.push(nnext);
               //pf("%d\n",temp);
			}
		}

        if(i<3||(i<7&&i>3))
		{
		    struct stud nnext;
		    change(cur,nnext);
		    swap(nnext.a[i],nnext.a[i+1]);
		    int temp=fdd(nnext);

		    if(!vis[temp])
			{
				q.push(nnext);
              // pf("%d\n",temp);
			}
		}

		if(i<4)
		{
			struct stud nnext;

		    change(cur,nnext);
		    swap(nnext.a[i],nnext.a[i+4]);
		    int temp=fdd(nnext);

		    if(!vis[temp])
			{
				q.push(nnext);
              // pf("%d\n",temp);
			}
		}
		else
		{
			struct stud nnext;
		    change(cur,nnext);
		    swap(nnext.a[i],nnext.a[i-4]);
		    int temp=fdd(nnext);
		    if(!vis[temp])
			{
				q.push(nnext);
               //pf("%d\n",temp);
			}

		}
//		t++;
//	  if(t==4)
//      return 0;
	}
   return -1;
}
int main()
{
	int i,j;
	stud cur;
	while(~scanf("%d",&cur.a[0]))
	{
		fre(i,1,8)
		  scanf("%d",&cur.a[i]);
      hello=0;
      hello=fdd(cur);

      fre(i,0,8)
        cur.a[i]=i;

	  printf("%d\n",bfs(cur));
	}
   return 0;
}

/*

0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
1 2 3 4 5 6 7 0

*/