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

struct Puzzle {
    int cell[8];
    int cur0, pre0, count;

    int score() {
        for (int i = 0; i < 8; i++) {
            if (i != cell[i]) return 1;
        }
        return 0;
    }
};

const int ofst[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

int main() {
    
    while (cin) {
        queue<Puzzle*> q;
        Puzzle *p = new Puzzle;
        for (int i = 0; i < 8; i++) {
            cin >> p->cell[i];
            if (p->cell[i] == 0) p->cur0 = i;
        }
        cin.ignore();
        p->count = 0;
        p->pre0 = -1;
        q.push(p);
        while (!q.empty()) {
            p = q.front();
            q.pop();
            if (p->score() == 0) {
                break;
            }

            for (int i = 0; i < 4; i++) {
                int x = (p->cur0 % 4) + ofst[i][0];
                int y = (p->cur0 / 4) + ofst[i][1];
                if (x < 0 || 4 <= x || y < 0 || 2 <= y) continue;
                int n0 = y * 4 + x;
                if (n0 == p->pre0) continue;
                Puzzle *np = new Puzzle;
                memcpy(np->cell, p->cell, sizeof(np->cell));
                np->cell[p->cur0] = np->cell[n0];
                np->cell[n0] = 0;
                np->count = p->count + 1;
                np->cur0 = n0;
                np->pre0 = p->cur0;
                q.push(np);
            }
            delete p;
        }
        cout << p->count << endl;
    }
    return 0;
}