#include <iostream>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
const int MAX=20;
struct node
{
    char ch[MAX];
    int s;
    int step;
};


char sh[MAX];
int fx[]={1,-1,4,-4};
bool check(int a,int b)
{
    if(b<0||b>7)
        return false;
    if(a==3&&b==4)
        return false;
    else if(a==4&&b==3)
        return false;
    else
        return true;
}
int  bfs(int star,char *ss)
{
    map<string ,int >s;
    s[sh]=1;
    node a;
    queue<node>Q;
    strcpy(a.ch,sh);
    a.s=star;
    a.step=0;
    Q.push(a);

    while(!Q.empty())
    {
        node p=Q.front();
        Q.pop();
        if(strcmp(p.ch,"01234567")==0)
            {
                return p.step;
            }
        for(int i=0;i<4;++i)
        {
            int tx=p.s+fx[i];

            if(!check(p.s,tx))
                continue;
            node b;
            strcpy(b.ch,p.ch);
            swap(b.ch[p.s],b.ch[tx]);
            if(s.find(b.ch)==s.end())
            {
                s[b.ch]=p.step+1;
                b.s=tx;
                b.step=p.step+1;
                Q.push(b);
            }
        }
    }
}
int main()
{
    while(scanf("%c %c %c %c %c %c %c %c",&sh[0],&sh[1],&sh[2],&sh[3],&sh[4],&sh[5],&sh[6],&sh[7])!=EOF)
    {

        getchar();
        int star;
        sh[8]='\0';int i;
        for(i=0;sh[i];++i)
            if(sh[i]=='0')
                star=i;

        printf("%d\n",bfs(star,sh));
//        map<string,int >::iterator ite;
//        for(ite =s.begin();ite!=s.end();++ite)
//            s.erase(ite);
       // printf("%d\n",s["01234567"]);
    }
    return 0;
}