#include <iostream>
#include <cstring>
#include <queue>

#define SIZE (8)
#define STATE_SIZE (40320)

int co[SIZE] = {5040, 720, 120, 24, 6, 2, 1, 0};

int movablePairs[SIZE][4] = {
    {1,4,-1,-1}, {0,2,5,-1}, {1,3,6,-1}, {2,7,-1,-1},
    {0,5,-1,-1}, {1,4,6,-1}, {2,5,7,-1}, {3,6,-1,-1}
};

bool isVisit[STATE_SIZE];

typedef struct {
    short state[SIZE];
    int depth;
    int movablePiecePoint;
    int point;
    bool isRoot;
    int stateCode;
} puzzle;


int calcStateCode(short state[])
{
    int stateCode = 0;
    for(int i=SIZE-2; i>=0; i--){
        int c=0;
        for(int j=SIZE-1; j-i>0; j--){
            if (state[i] > state[j]) {
                c++;
            }
        }
        stateCode += (co[i]*c);
    }
    return stateCode;
}

/*
 * 根ノード生成
 */
void puzzleInit(puzzle* p, short state[])
{
    memcpy(p->state, state, sizeof(short)*SIZE);
    p->isRoot = true;
    p->depth = 0;
    p->point = -1;
    p->stateCode = calcStateCode(p->state);   
    for (int i=0; i<SIZE; i++) {
        if(0 == p->state[i]){
            p->movablePiecePoint = i;
            return;
        }
    }
    p->movablePiecePoint = -1;
}


/*
 * 子ノード生成
 */
void puzzleSetParam(puzzle* nextP, puzzle* parentP, int nextMove)
{
    nextP->isRoot = false;
    nextP->depth = parentP->depth+1;
    nextP->point = parentP->movablePiecePoint;
    nextP->movablePiecePoint = nextMove;
    
    // stateのswap
    memcpy(nextP->state, parentP->state, sizeof(short)*SIZE);
    short tmp = nextP->state[nextP->point];
    nextP->state[nextP->point] = nextP->state[nextMove];
    nextP->state[nextMove] = tmp;
    
    nextP->stateCode = calcStateCode(nextP->state);  
}

int solve(short input[])
{
    std::queue<puzzle> qu;
    for (int i=0; i<STATE_SIZE; i++) {
        isVisit[i] = false;
    }

    puzzle init;
    puzzleInit(&init, input);
    qu.push(init);
    while (!qu.empty()) {
        puzzle now = qu.front();
        qu.pop();
        if (0 == now.stateCode) {
            return now.depth;
        }
        
        if (!isVisit[now.stateCode]) {
            for (int i=0; movablePairs[now.movablePiecePoint][i]>=0; i++){
                int nextMove = movablePairs[now.movablePiecePoint][i];
                if(now.isRoot || (!now.isRoot && now.point!=nextMove)){
                    puzzle nextP;
                    puzzleSetParam(&nextP, &now, nextMove);
                    qu.push(nextP);
                }
            }
            isVisit[now.stateCode] = true;
        }
    }
    return -1;
}

int main(int argc, const char * argv[])
{
    while (true) {
        short input[SIZE];
        for(int i=0; i<SIZE; i++){
            std::cin >> input[i];
        }
        std::cout << solve(input)<< std::endl;
    }    
    return 0;
}