#include<bits/stdc++.h>
using namespace std;
map<string,int> mymap;
int ds[4]={1,-1,4,-4};
void bfs()
{
    mymap["01234567"]=0;
    queue<string> q;
    q.push("01234567");
    while(!q.empty())
    {
        int zero;
        string x=q.front();
        q.pop();
        for(int i=0;i<8;++i)
        {
            if(x[i]=='0')
                zero=i;
        }
        for(int i=0;i<4;++i)
        {
            int t=zero+ds[i];
            if(t>=0&&t<8&&!(zero==3&&i==0)&&!(zero==4&&i==1))
            {
                string now=x;
                swap(now[zero],now[t]);
                if(mymap.find(now)==mymap.end())
                {
                    mymap[now]+=mymap[x]+1;
                    q.push(now);
                }
            }
        }

    }
}
int main()
{
    bfs();
    string s;
    while(getline(cin,s))
    {
        s.erase(remove(s.begin(),s.end(),' '),s.end());
        cout<<mymap[s]<<endl;
    }
}

