#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
int dr[][2]={-1,0,1,0,0,1,0,-1};
struct node{
    int mp[4][4];
    int x,y,step;
};
int num[10];
int temp;
node st;
queue<node>q;
map<int,int>vis;
int tran(node x)
{
    int sum=0;
    REPF(i,1,2)
      REPF(j,1,4)
        sum=sum*10+x.mp[i][j];
    return sum;
}
bool ok(int x,int y)
{
    if(x<1||x>2||y<1||y>4) return false;
    return true;
}
int BFS()
{
     vis.clear();node ed;
     queue<node>q;
     st.step=0;vis[tran(st)]=1;
     q.push(st);
     while(!q.empty())
     {
          st=q.front();
          q.pop();
          if(tran(st)==1234567)
             return st.step;
          for(int i=0;i<4;i++)
          {
              int xx=st.x+dr[i][0];
              int yy=st.y+dr[i][1];
              if(ok(xx,yy))
              {
                  REPF(k,1,2)
                    REPF(j,1,4)
                     ed.mp[k][j]=st.mp[k][j];
                  ed.mp[xx][yy]=0;
                  ed.mp[st.x][st.y]=st.mp[xx][yy];
                  ed.x=xx;ed.y=yy;
                  ed.step=st.step+1;
                  if(!vis[tran(ed)])
                  {
                      q.push(ed);
                      vis[tran(ed)]=1;
                  }
              }
          }
     }
}
int main()
{
    while(~scanf("%d",&num[1]))
    {
        for(int i=2;i<=8;i++)
           scanf("%d",&num[i]);
        int cnt=1;
        REPF(i,1,2)
           REPF(j,1,4)
           {
               st.mp[i][j]=num[cnt];
               if(num[cnt]==0)
               {
                   st.x=i;
                   st.y=j;
               }
               cnt++;
           }
        printf("%d\n",BFS());
    }
    return 0;
}