#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
struct ST
{
    int x;
    int deep;
    string s;
};
int mx[4] = {1, -1, 4, -4};
int a[10][10];
int ans =0;
map<string, int> vis;
//int ans = 0;
void bfs()
{
    ST star;
    star.x = 0;
    star.deep = 0;
    star.s = "01234567";
    queue<ST> q;
    q.push(star);
    vis[star.s] = 0;
    while(!q.empty())
    {
        ST u = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            if(i == 0 && u.x == 3)
                continue;
            if(i == 1 && u.x == 4)
                continue;
            ST v;
            v.x = u.x + mx[i];
            v.s = u.s;
            if(v.x >=0 && v.x < 8)
            {

                swap(v.s[u.x], v.s[v.x]);
                if(!vis[v.s]){
                    vis[v.s] = u.deep + 1;
                    v.deep = u.deep + 1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    bfs();
    ios::sync_with_stdio(false);
    string s;
    while(getline(cin, s))
    {
        string t = "";
        int L = s.size();
        for(int i = 0;i < L; i = i + 2)
        {
            t += s[i];
        }
        cout << vis[t] << endl;

    }
    return 0;
}

