#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
map <string,int> dp;
string s;
int dx[]={-1,1,-4,4};
void bfs()
{
    queue<string> que;
    que.push("01234567");
    dp["01234567"]=0;
    string now,next;
    while(que.size())
    {
        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 nx=p+dx[i];
           if(nx<0||nx>=8||(nx==3&&i==1)||(nx==4&&i==0)) continue;
           next=now;
           swap(next[p],next[nx]);
           if(dp.find(next)==dp.end())
           {
               dp[next]=dp[now]+1;
               que.push(next);
           }
       }
    }
}
int main(int argc, char *argv[])
{
    bfs();
    string line;
	while (getline(cin, line))
	{
		line.erase(remove(line.begin(), line.end(), ' '), line.end());
		cout << dp[line] << endl;
	}
    return 0;
}