#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <set>
#include <queue>
#include <stack>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct{int to; int cost;};

map<string, int> dp;

int d[4] = {1, -1, 4, -4};

bool check(int zero, int change)
{
    return change >= 0 && change <= 7 && !(zero == 3 && change == 4) && !(zero == 4 && change == 3);
}

void dfs(string puzzle)
{
    queue<string> que;
    que.push(puzzle);

    while(!que.empty())
    {
        string str = que.front(); que.pop();
        int zero = str.find("0");

        for(int i = 0; i < 4; i++)
        {
            int change = zero + d[i];

            if(check(zero, change))
            {
                string str2 = str;
                swap(str2[zero], str2[change]);

                if(dp.find(str2) == dp.end())
                {
                    dp[str2] = dp[str] + 1;
                    que.push(str2);
                }
            }
        }
        
    }

    
}


int main()
{
    char input[8];
    while(cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4] >> input[5]
    >> input[6] >> input[7])
    {
        dfs(input);
        cout << dp[input] << endl;
    }  
    return 0;
}
