/*If I get TLE , it is good.If I get AC,it's NICE !*/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int INF=2147483647;
const int MAXN=1e7+10;
const ll mod=1e9+7;
using namespace std;
#define boost ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
map <string,int> step;
int dir[4]={1,-1,4,-4};

void bfs()
{
    queue <string> que;
    que.push("01234567");
    step["01234567"]=0;
    while(!que.empty())
    {
        string s;
        s=que.front();
        int pos=0;
        for(int i=0;i<=7;i++)
        {
            if(s[i]=='0')
            {
                pos=i;
                break;
            }
        }
        que.pop();
        for(int i=0;i<4;i++)
        {
            int np=pos+dir[i];
            if(np==3 && i==0)    continue;
            if(np==4 && i==1)    continue;
            if(np<0 || np >=8)   continue;
            string temp=s;
            swap(temp[pos],temp[np]);
            if(step.find(temp)==step.end())
            {
                que.push(temp);
                step[temp]=step[s]+1;
            }
        }
    }
}

int main(void)
{
    bfs();
    string s;
    while(getline(cin,s)){
       s.erase(remove(s.begin(),s.end(),' '),s.end());
       cout<<step[s]<<endl;
    }
}
/*unsigned   int   0?4294967295
int   -2147483648?2147483647
unsigned long 0?4294967295
long   -2147483648?2147483647
long long的最大?：9223372036854775807
long long的最小?：-9223372036854775808
unsigned long long的最大?：1844674407370955161*/
//s.erase(remove(s.begin(),s.end(),' '),s.end());?入不?定字符?度，同?去空格