#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

char str[8];
char fin[8]={0,1,2,3,4,5,6,7};
int dir[4]={-1,1,-4,4};

struct stat={
    char temp[8];
    int num;
    stat(char* tt, int nn){
        for(int i=0; i<8; i++) temp[i]==tt[i];
        num =nn;
    }
}
bool equal(char* p1, char* p2){
    for(int i=0; i<8; i++){
            if(p1[i]!=p2[i]) return false;
    }
    return true;
}

int bfs(char* s){
    queue<stat> q;
    q.push(stat(s, 0));
    
    while(q.size()){
        stat cur=q.front(); q.pop();
        if(equal(cur.temp,fin)) return cur.step;
        int zx;
        for(int i=0; i<8; i++){ if(cur.temp[i]=='0') {zx=i; break;}}
        for(int i=0; i<4; i++){
            int nx = zx+dir[i];
            if(nx>=0&& nx<8){
                char t[8];
                strcpy(t, cur.temp);
                char a=t[zx]; t[zx]=t[nx]; t[nx]=a;
                q.push(stat(t, cur.step+1));
            }
        }
    }
}

int main(){
    while(cin>>str){
        cout<<bfs(str)<<endl;
    }   
}
