#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define PI acos(-1.0)
#define M 1000005  //10^6
#define eps 1e-8
#define LL long long
#define moo 1000000007
#define INF -999999999
using namespace std;
int a[10];
void swapp(int x,int y)
{
    int t=a[x];
    a[x]=a[y];
    a[y]=t;
    return ;
}
int ju(int x,int y)
{
    if(x<=4)
    {
        if(y<=4)
            return abs(y-x);
        else
            return abs(y-(x+4))+1;
    }
    else
    {
        if(y<=4)
            return abs(y+4-x)+1;
        else
            return abs(x-y);
    }
}
int main()
{
    while(scanf("%d",&a[1])!=EOF)
    {
        for(int i=2;i<=8;i++)
            scanf("%d",&a[i]);
        int ans=0;
        int now=0;
        for(int i=0;i<=7;i++)
        {
            int pick;
            for(int j=1;j<=8;j++)
            {
                if(a[j]==i)
                {
                    pick=j;
                    break;
                }
            }
            int flag=0;
            int nima=pick;
            while(pick!=i+1)
            {
                flag=1;
                if(pick<i+1)
                {
                    if(pick<=4&&i+1>=5)
                    {
                        swapp(pick,pick+4);
                        pick+=4;
                    }
                    else
                    {
                        swapp(pick,pick+1);
                        pick+=1;
                    }
                }
                else
                {
                    if(pick-4>=i+1)
                    {
                        swapp(pick,pick-4);
                        pick-=4;
                    }
                    else if(pick>=5&&i+1<=4)
                    {
                        swapp(pick,pick+1);
                        pick++;
                    }
                    else
                    {
                        swapp(pick,pick-1);
                        pick--;
                    }
                }
                ans++;
            }
            //cout<<flag<<ends<<now<<ends<<pick<<ends<<ends;
            if(flag==1)
            {
                if(now!=0)
                    ans+=ju(now,nima);
                now=pick;
            }

            //cout<<i<<ends<<ans<<endl;
            //for(int i=1;i<=8;i++)
                //cout<<a[i]<<ends;
            //cout<<endl;
        }
        cout<<ans<<endl;
    }
}