#include <iostream>
#include <cstdio>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;

using Data = struct{
    int pos;
    string puzzle;
    int t;
};

constexpr int dx[] = {-1, 1, -4, 4};
const string correct{"01234567"};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string puzzle{"********"};
    int sp;
    Data cd;

    while (cin >> puzzle[0] >> puzzle[1] >> puzzle[2] >> puzzle[3] >> puzzle[4] >> puzzle[5] >> puzzle[6] >> puzzle[7]) {
        queue<Data> q;
        sp = distance(begin(puzzle), find(begin(puzzle), end(puzzle), '0'));
        q.push(Data{sp, puzzle, 0});

        set<string> visited;

        while(!q.empty()){
            cd = q.front();
            q.pop();

            if(cd.puzzle == correct){
                cout << cd.t << endl;
                break;
            }

            for(int i = 0, npos; i < 4; ++i){
                npos = cd.pos+dx[i];

                if(npos < 0 || npos > 7 || (cd.pos == 3 && npos == 4) || (cd.pos == 4 && npos == 3)) continue;

                string npuzzle = cd.puzzle;
                swap(npuzzle[cd.pos], npuzzle[npos]);

                if(visited.find(npuzzle) != end(visited)) continue;
                visited.emplace(npuzzle);

                q.push(Data{npos, npuzzle, cd.t+1});
            }
        }
    }
}
