#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<vector<int>, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int d[] = {-1, -4, 1, 4};
map<vector<int>, int> bfs(vector<int> goal)
{
    queue<P> que;
    map<vector<int>, int> res;
    que.push(P(goal, 0));
    res[goal] = 0;
    while(que.size())
    {
        vector<int> now = que.front().first;
        int cnt = que.front().second;
        que.pop();
        int zero = -1;
        for(int i = 0; i < now.size(); i++) if(now[i] == 0) zero = i;
        for(int i = 0; i < 4; i++)
        {
            int u = zero + d[i];
            if(u > 7 || u < 0) continue;
            if(zero == 3 && u == 4) continue;
            if(zero == 4 && u == 3) continue;
            swap(now[zero], now[u]);
            if(res.count(now) == 0 || res[now] > cnt + 1)
            {
                que.push(P(now, cnt + 1));
                res[now] = cnt + 1;
            }
            swap(now[u], now[zero]);
        }
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<int> goal;
    for(int i = 0; i <= 7; i++) goal.push_back(i);
    map<vector<int>, int> mp = bfs(goal);
    string s;
    while(getline(cin, s))
    {
        vector<int> v;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == ' ') continue;
            v.push_back(s[i] - '0');
        }
        cout << mp[v] << endl;
    }
    return 0;
}


