#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        string s(8, ' ');
        for(int i=0; i<8; ++i){
            if(!(cin >> s[i]))
                return 0;
        }

        set<string> history;
        history.insert(s);
        queue<string> q;
        q.push(s);
        int ret = 0;
        for(;;){
            int n = q.size();
            bool ok = false;
            for(int i=0; i<n; ++i){
                string s1 = q.front();
                q.pop();
                if(s1 == "01234567"){
                    ok = true;
                    break;
                }

                int j = s1.find('0');
                if(j < 4){
                    string s2 = s1;
                    swap(s2[j], s2[j+4]);
                    if(history.find(s2) == history.end()){
                        q.push(s2);
                        history.insert(s2);
                    }
                }else{
                    string s2 = s1;
                    swap(s2[j], s2[j-4]);
                    if(history.find(s2) == history.end()){
                        q.push(s2);
                        history.insert(s2);
                    }
                }
                if(j % 4 != 0){
                    string s2 = s1;
                    swap(s2[j], s2[j-1]);
                    if(history.find(s2) == history.end()){
                        q.push(s2);
                        history.insert(s2);
                    }
                }
                if(j % 4 != 3){
                    string s2 = s1;
                    swap(s2[j], s2[j+1]);
                    if(history.find(s2) == history.end()){
                        q.push(s2);
                        history.insert(s2);
                    }
                }
            }

            if(ok){
                cout << ret << endl;
                break;
            }
            ++ ret;
        }
    }
}