#include <cassert>
#include <iostream>
#include <set>
#include <queue>
#include <vector>
using namespace std;

enum Direction {
    LEFT = -1,
    DOWN = 4,
    UP = -4,
    RIGHT = 1,
};

constexpr Direction directions[4] = {LEFT, DOWN, UP, RIGHT};

struct status {
    int v;
    int cnt;

    status() {}
    ~status() {}

    status(int a, int b): v(a), cnt(b) {}

    int index(int i) {
        return (v >> (3 * (7 - i))) % 8;
    }

    int find(const int a) {
        int x = v;
        for (int j = 0; j < 8; ++j) {
            if(x % 8 == a) {
                return 7 - j;
            }

            x >>= 3;
        }

        return -1;
    }

    int move_zero(const Direction d) {
        int zi = find(0);

        if (d == LEFT && zi % 4 > 0) {
            swap(zi, zi + LEFT);
            return 0;
        } else if (d == DOWN && zi < 4) {
            swap(zi, zi + DOWN);
            return 0;
        } else if (d == UP && zi >= 4) {
            swap(zi, zi + UP);
            return 0;
        } else if (d == RIGHT && zi % 4 < 3) {
            swap(zi, zi + RIGHT);
            return 0;
        }

        return -1;
    }

    private:
    void swap(int a, int b) {
        assert(0 <= a && a < 8);
        assert(0 <= b && b < 8);

        int va = index(a);
        int vb = index(b);

        v += (-va + vb) * (1 << (21 - 3 * a));
        v += (-vb + va) * (1 << (21 - 3 * b));
    }
};

constexpr int GOAL = 
(0 << 21) + 
(1 << 18) + 
(2 << 15) + 
(3 << 12) + 
(4 << 9) + 
(5 << 6) + 
(6 << 3) + 
(7 << 0);

set<int> visited;
queue<status> que;

int solve(vector<int>& v) {
    int a = 0;
    for (const int& e : v) {
        a = 8 * a + e;
    }

    queue<status> empty;
    swap(que, empty);

    que.push(status(a, 0));
    visited.insert(a);

    while (!que.empty()) {
        status s = que.front(); que.pop();

        if (s.v == GOAL) {
            return s.cnt;
        }

        for (const Direction& d : directions) {
            int move_status = s.move_zero(d);

            if (move_status == 0) {
                if (visited.find(s.v) == end(visited)) {
                    que.push(status(s.v, s.cnt + 1));
                    visited.insert(s.v);
                }
                s.move_zero((Direction)-d);
            }
        }
    }
}

int main() {
    int b;
    vector<int> v(8);
    while (cin >> b) {
        v[0] = b;

        for (int j = 1; j < 8; ++j) {
            cin >> v[j];
        }

        cout << solve(v) << endl;
    }
    return 0;
}