#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <set>
#include <utility>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

typedef pair<string,int> P;
string str;
map<string,int> table;
int dir[]= {-1,1,-4,4}; //移?方向
void bfs()
{
    queue<P> q;
    string s,s_next;
    P temp;
    int cur,next;
    q.push(P("01234567",0));
    table["01234567"]=0;
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        s=temp.first;
        cur=temp.second;
        for(int i=0; i<4; i++)//将0向四个方向移?
        {
            next=cur+dir[i];
            str=s;
            swap(str[cur],str[next]);
            if(next>=0&&next<8&&!(cur==3&&next==4)&&!(cur==4&&next==3)&&table.find(str)==table.end())
            {                           //判断移?是否合法及?状?是否已存在
                table[str]=table[s]+1;
                q.push(P(str,next));
            }
        }
    }
}
int main()
{
    //freopen("input.txt","r",stdin);
    char sstr[50];
    bfs();
    while(gets(sstr))
    {
        for(int i=1; i<8; i++)
            sstr[i]=sstr[i*2];
        sstr[8]='\0';
        str=sstr;
        printf("%d\n",table[str]);
    }
}