#include <iostream>
#include <cstdio>
#include <queue>
#include <string>
#include <map>
#include <algorithm>
using namespace std;

map<string,int> dp;
int dir[4]={-4,4,-1,1};//上下左右
void bfs()
{
    queue<string> que;
    dp["01234567"]=0;
    que.push("01234567");

    while(!que.empty())
    {
        string now=que.front();que.pop();
        int p=0;
        for(int i=0;i<8;i++)
        {
            if(now[i]=='0')
            {
                p=i;
                break;
            }
        }

        for(int i=0;i<4;i++)
        {
            int t=p+dir[i];
            if(t<=7&&t>=0&&!(p==3&&i==3)&&!(p==4&&i==2))
            {
                string next=now;
                swap(next[p],next[t]);
                if(dp.find(next)==dp.end())
                {
                    que.push(next);
                    dp[next]= dp[now]+1;
                }
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    bfs();
    string line;
    while (getline(cin, line))
    {
        line.erase(remove(line.begin(), line.end(), ' '), line.end());
        cout << dp[line] << endl;
    }
    return 0;
}

