#include <cstdio>
#include <queue>
#include <map>
#include <iostream>
#include <set>
using namespace std;
int num[2][4],k,t,x,y;
string str;
map<string, bool> mp;
int dir[4] = {1,-1,4,-4};
struct node
{
    string s;
    int step ;
};
int bfs()
{
    mp.clear();
    node now;
    now.s = str;
    now.step = 0;
    queue<node> Q;
    mp[str] == true;
    Q.push(now);
    while(!Q.empty())
    {
        node pre = Q.front();
        Q.pop();
        int pos;
        string s2 = pre.s;
        if(s2 == "01234567") return pre.step;

        for(int i = 0; i < s2.length(); i++)
            if(s2[i] == '0') pos = i;

        for(int i = 0; i < 4; i++)
        {
            int pos2 = pos + dir[i];
            if((i == 0 && pos == 3) || pos2 >= 8 || pos2 < 0 ||
               (i == 1 && pos == 4)) continue;
            string u = s2;
            swap(u[pos], u[pos2]);
            if(mp[u]) continue;
            mp[u] = true;
            now = pre;
            now.s = u;
            now.step = pre.step+1;
            Q.push(now);
        }
    }
}
void solve()
{
    printf("%d\n", bfs());
}
int main()
{
    str = "";
    //freopen("in.txt", "r", stdin);
    k = 0;
    while(~scanf("%d",  &t))
    {
        str += t+'0';
        k++;
        if(k == 8)
        {
            solve();
            k = 0;
            str = "";
        }
    }
    return 0;
}