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

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

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

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 stateCode;
} puzzle;

void puzzleSetParam(short state[], int depth, puzzle* nextP , int point1, int point2)
{
    memcpy(nextP->state, state, sizeof(short)*SIZE);
    if (point1 != point2) {
        short tmp = nextP->state[point1];
        nextP->state[point1] = nextP->state[point2];
        nextP->state[point2] = tmp;
    }
    nextP->depth = depth+1;
    // stateCodeと0の位置の計算
    int tmpStateCode = 0;
    int tmpMovablePiecePoint = -1;
    for (int i=0; i<SIZE; i++) {
        if(0 == nextP->state[i]){
            tmpMovablePiecePoint = i;
        }
        int c=0;
        for (int j=i; j<SIZE; j++) {
            if (nextP->state[i] > nextP->state[j]) {
                c += 1;
            }
        }
        tmpStateCode += (co[SIZE-1-i]*c);
    }
    nextP->stateCode = tmpStateCode;
    nextP->movablePiecePoint = tmpMovablePiecePoint;

    return;
}

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

    puzzle init;
    puzzleSetParam(input, -1, &init, 0, 0);
    
    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];
                puzzle nextP;
                puzzleSetParam(now.state, now.depth, &nextP, now.movablePiecePoint, 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;
}