#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

using Point = pair<int, int>;

const int MAX_N = 8;
const int MAX_ID = 40320;
const int GOAL_ID = 0;
const int INF = 0x3f3f3f3f;
const int RULE_NUM = 3;
const int RULE[MAX_N][RULE_NUM] = {{1, 4, -1}, {0, 2, 5}, {1, 3, 6}, {2, 7, -1},
                                   {0, 5, -1}, {1, 4, 6}, {2, 5, 7}, {3, 6, -1}};
const int FACTORS[MAX_N] = {1, 1, 2, 6, 24, 120, 720, 5040};

int ConvertVectorToId(const vector<int>& numbers) {
    bool is_used[MAX_N];
    memset(is_used, false, sizeof(is_used));

    int id = 0;
    for (size_t i = 0; i < MAX_N; i++) {
        int rank = 0;
        for (size_t j = 0; j < numbers[i]; j++) {
            if (!is_used[j]) { rank++; }
        }
        id += rank * FACTORS[MAX_N - i - 1];
        is_used[numbers[i]] = true;
    }

    return id;
}

vector<int> ConvertIdToVector(int id) {
    bool is_used[MAX_N];
    memset(is_used, false, sizeof(is_used));

    vector<int> numbers;
    for (int i = MAX_N - 1; i >= 0; i--) {
        int rank = id / FACTORS[i];
        id %= FACTORS[i];
        int count = 0;
        int num = 0;
        while (count < rank) {
            if (!is_used[num++]) { count++; }
        }
        while (is_used[num]) { num++; }
        numbers.push_back(num);
        is_used[num] = true;
    }

    return numbers;
}

int BFS(vector<int>& numbers, int zero_pos) {
    int id = ConvertVectorToId(numbers);

    int dists[MAX_ID];
    int paths[MAX_ID];
    for (int i = 0; i < MAX_ID; i++) {
        dists[i] = INF;
        paths[i] = -1;
    }
    queue<int> que;
    que.push(id);
    dists[id] = 0;
    paths[id] = -2;

    while (que.size()) {
        id = que.front();
        numbers = ConvertIdToVector(id);
        que.pop();
        if (id == GOAL_ID) { break; }

        zero_pos = distance(numbers.begin(), find(numbers.begin(), numbers.end(), 0));
        for (size_t i = 0; i < RULE_NUM; i++) {
            int new_zero_pos = RULE[zero_pos][i];
            if (new_zero_pos < 0) { continue; }
            vector<int> new_numbers = numbers;
            swap(new_numbers[zero_pos], new_numbers[new_zero_pos]);
            int new_id = ConvertVectorToId(new_numbers);
            if (dists[new_id] == INF) {
                que.push(new_id);
                dists[new_id] = dists[id] + 1;
                paths[new_id] = id;
            }
        }
    }

    return dists[GOAL_ID];
}

int main() {
    string line;
    while (getline(cin, line)) {
        stringstream ss;
        ss << line;
        vector<int> numbers(MAX_N);
        int zero_pos = 0;
        for (size_t i = 0; i < MAX_N; i++) {
            ss >> numbers[i];
            if (numbers[i] == 0) {
                zero_pos = i;
            }
        }
        cout << BFS(numbers, zero_pos) << endl;
    }

    return 0;
}