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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

vector<int> fin;
int dx[4] = {-1, 1, -4, 4};
typedef pair<int, vector<int> > pp;


int main()
{
    REP(8) fin.push_back(i);
    int n[10];
    while(cin >> n[0])
    {
        REP(7) cin >> n[i+1];

        vector<int> start;
        REP(8) start.push_back( n[i]);


        //REP(8) cout << fin[i] << " " ; cout << endl;
        queue<pp> que;
        que.push(pp(0, start));
        map<vector<int>, int > ind;

        int ans = -1;
        while(!que.empty())
        {
            pp q = que.front(); que.pop();
            if(ind.find(q.second) != ind.end()) continue;

            ind[q.second] = q.first;
            
            //REP(8) cout << q.second[i] << " " ; cout << endl;
            if(q.second == fin){ans = q.first; break;}
            
            int p0 = 0;
            REP(8)if(q.second[i] == 0)p0 = i;

            

            REP(4)if(p0 + dx[i] >= 0 && p0 + dx[i] < 8)
            {
                if(p0 == 3 && dx[i] == 1) continue;
                if(p0 == 4 && dx[i] == -1) continue;
                vector<int> next = q.second;
                next[p0] = q.second[p0 + dx[i] ];
                next[p0+dx[i]] = q.second[p0];
                que.push(pp(q.first + 1 , next));
                //cout << "phaze " << q.first + 1 <<endl;
            }
        }
        cout << ind[fin] << endl;
    }

    return 0;
}