#include <cstdlib>
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

/*
 * 
 */

class PZ{
public:
    int p[8];
    int n;
    int zp; //zp is show us where zero is.
    
    bool is_over(){
        for(int i=0;i<8;i++){
            if(p[i]!=i) return false;
        }
        return true;
    }
    void rot0(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot04(); nex1.n++; nex1.zp = 4; que->push(nex1);
        PZ nex2 = *this; nex2.rot01(); nex2.n++; nex2.zp = 1; que->push(nex2);        
    }
    void rot1(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot15(); nex1.n++; nex1.zp = 5; que->push(nex1);
        PZ nex2 = *this; nex2.rot01(); nex2.n++; nex2.zp = 0; que->push(nex2);
        PZ nex3 = *this; nex3.rot12(); nex3.n++; nex3.zp = 2; que->push(nex3);        
    }
    void rot2(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot26(); nex1.n++; nex1.zp = 6; que->push(nex1);
        PZ nex2 = *this; nex2.rot12(); nex2.n++; nex2.zp = 1; que->push(nex2);
        PZ nex3 = *this; nex3.rot23(); nex3.n++; nex3.zp = 3; que->push(nex3);        
    }
    void rot3(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot23(); nex1.n++; nex1.zp = 2; que->push(nex1);
        PZ nex2 = *this; nex2.rot37(); nex2.n++; nex2.zp = 7; que->push(nex2);        
    }
    void rot4(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot04(); nex1.n++; nex1.zp = 0; que->push(nex1);
        PZ nex2 = *this; nex2.rot45(); nex2.n++; nex2.zp = 5; que->push(nex2);        
    }
    void rot5(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot15(); nex1.n++; nex1.zp = 1; que->push(nex1);
        PZ nex2 = *this; nex2.rot45(); nex2.n++; nex2.zp = 4; que->push(nex2);
        PZ nex3 = *this; nex3.rot56(); nex3.n++; nex3.zp = 6; que->push(nex3);        
    }
    void rot6(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot26(); nex1.n++; nex1.zp = 2; que->push(nex1);
        PZ nex2 = *this; nex2.rot56(); nex2.n++; nex2.zp = 5; que->push(nex2);
        PZ nex3 = *this; nex3.rot67(); nex3.n++; nex3.zp = 7; que->push(nex3);        
    }
    void rot7(queue<PZ> *que){
        PZ nex1 = *this; nex1.rot37(); nex1.n++; nex1.zp = 3; que->push(nex1);
        PZ nex2 = *this; nex2.rot67(); nex2.n++; nex2.zp = 6; que->push(nex2);        
    }
    void rot04(){
        swap(p[0],p[4]);
    }
    void rot15(){
        swap(p[1],p[4]);
    }
    void rot26(){
        swap(p[2],p[6]);
    }
    void rot37(){
        swap(p[3],p[7]);
    }
    void rot01(){
        swap(p[0],p[1]);
    }
    void rot12(){
        swap(p[1],p[2]);
    }
    void rot23(){
        swap(p[2],p[3]);
    }
    void rot45(){
        swap(p[4],p[5]);
    }
    void rot56(){
        swap(p[5],p[6]);
    }
    void rot67(){
        swap(p[6],p[7]);
    }
};

int main(int argc, char** argv) {
    int a;
    while(cin>>a){
        PZ now;
        if(a==0)now.zp = 0;
        now.p[0]=a;
        for(int i=1;i<=7;i++){
            cin >> a;
            if(a==0)now.zp = i;
            now.p[i] = a;
        }
        now.n = 0;
        
        queue<PZ> que;
        que.push(now);
        
        while(!que.empty()){
            now = que.front();
            que.pop();
            
            if(now.is_over()){
                cout << now.n << endl;
                break;
            }
            
            switch(now.zp){
                case 0:
                    now.rot0(&que);
                    break; 
                case 1:
                    now.rot1(&que);
                    break;   
                case 2:
                    now.rot2(&que);
                    break;   
                case 3:
                    now.rot3(&que);
                    break;   
                case 4:
                    now.rot4(&que);
                    break;   
                case 5:
                    now.rot5(&que);
                    break;   
                case 6:
                    now.rot6(&que);
                    break;   
                case 7:
                    now.rot7(&que);
                    break;   
                default:
                    return 0;
            }
        }
    }
    return 0;
}