#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;

const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};

bool c (const vector<int> &v) {
    for (int i = 0; i < 8; i++) {
        if (i != v[i]) return false;
    }
    return true;
}

int main ()
{
    vector<int> v(8);
    while (cin >> v[0]) {
        for (int i = 1; i < 8; i++) {
            cin >> v[i];
        }

        queue<pair<vector<int>,int> > q;
        q.push(pair<vector<int>,int>(v,0));
        map<vector<int>,int> m;
        m[v] = 1;

        while (!c(q.front().first)) {
            int t[2][4];
            int x, y;
            for (int i = 0; i < 8; i++) {
                int tx = i / 4;
                int ty = i % 4;
                t[tx][ty] = (q.front().first)[i];
                if (t[tx][ty] == 0) {
                    x = tx;
                    y = ty;
                }
            }
            int n = q.front().second;
            q.pop();
            for (int i = 0; i < 4; i++) {
                int nx = dx[i] + x, ny = dy[i] + y;
                if (0 <= nx && nx < 2 && 0 <= ny && ny < 4) {
                    int tmp[2][4];

                    for (int j = 0; j < 8; j++) {
                        tmp[j / 4][j % 4] = t[j / 4][j % 4];
                    }

                    tmp[x][y] = t[nx][ny];
                    tmp[nx][ny] = t[x][y];

                    vector<int> sub(8, 0);
                    for (int j = 0; j < 2; j++) {
                        for (int k = 0; k < 4; k++) {
                            sub[j * 4 + k] = tmp[j][k];
                        }
                    }
                    if (m.find(sub) == m.end()) {
                        q.push(pair<vector<int>,int>(sub,n+1));
                        m[sub] = 1;
                    }
                }
            }
        }
        cout << q.front().second << endl;
    }

    return 0;
}