#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

unordered_map<string, int> dist;

struct A {
  string s;
  int d;
  int zero;
};

int main() {
  queue<A> que;
  que.push({string("01234567"), 0, 0});
  dist[string("01234567")] = 0;
  while(!que.empty()) {
    string arr = que.front().s;
    int d = que.front().d;
    int zero = que.front().zero;
    que.pop();
    do {
      int b;
      if(zero % 4 == 3) break;
      else b = zero + 1;
      string next = arr;
      swap(next[zero], next[b]);
      if(!dist.count(next)) {
        dist[next] = d + 1;
        que.push({next, d + 1, b});
      }
    } while(false);
    do {
      int b;
      if(zero % 4 == 0) break;
      else b = zero - 1;
      string next = arr;
      swap(next[zero], next[b]);
      if(!dist.count(next)) {
        dist[next] = d + 1;
        que.push({next, d + 1, b});
      }
    } while(false);

    do {
      int b;
      b = zero ^ 4;
      string next = arr;
      swap(next[zero], next[b]);
      if(!dist.count(next)) {
        dist[next] = d + 1;
        que.push({next, d + 1, b});
      }
    } while(false);
  }
  array<int, 8> arr;
  while(cin >> arr[0]) {
    for(int i = 1;i < 8;i++) cin >> arr[i];
    string s;
    for(auto x: arr) s.push_back((char)(x + (int)'0'));
    cout << dist[s] << endl;
  }
}

