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

#define SIZE 8

int coefficients[SIZE] = {10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};

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

int answerStateCode = 1234567;

class Puzzle
{
public:
    int state[SIZE];
    int depth;
    int movablePiecePoint;
    int stateCode;
    Puzzle(int state0[], int depth0);
};

Puzzle::Puzzle(int state0[], int depth0)
{
    memcpy(state, state0, sizeof(int)*SIZE);
    depth = depth0;
    stateCode = 0;
    for (int i=0; i<SIZE; i++) {
        if(state0[i] == 0){
            movablePiecePoint = i;
        }
        stateCode += coefficients[i]*state0[i];
    }
}

void move(int state[], int nextState[], int point1, int point2)
{
    memcpy(nextState, state, sizeof(int)*SIZE);
    int tmp = nextState[point1];
    nextState[point1] = nextState[point2];
    nextState[point2] = tmp;
    return;
}

bool isVisit[76543211];

int solve(int input[])
{
    queue<Puzzle> qu;
    for (int i=0; i<76543211; i++) {
        isVisit[i] = false;
    }
    
    Puzzle init = Puzzle(input, 0);
    qu.push(init);
    while (!qu.empty()) {
        Puzzle now = qu.front();
        qu.pop();
        if (answerStateCode == now.stateCode) {
            return now.depth;
        }
        
        if (!isVisit[now.stateCode]) {
            for (int i=0; movablePairs[now.movablePiecePoint][i]>=0 and i<3; i++){
                int nextMove = movablePairs[now.movablePiecePoint][i];
                int nextState[SIZE];
                move(now.state, nextState, now.movablePiecePoint, nextMove);
                Puzzle nextP = Puzzle(nextState, now.depth+1);
                qu.push(nextP);
            }
            isVisit[now.stateCode] = true;
        }
    }
    return -1;
}

int get()
{
    int input[SIZE];
    for(int i=0; i<SIZE; i++){
        cin >> input[i];
    }
     
    return solve(input);
}


int main(int argc, const char * argv[])
{
    while (true) {
        cout << get() << endl;
    }
    
    return 0;
}