#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
#include <unordered_set>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int t[10];
typedef pair<string, int> P;
unordered_set<string> memo;

// 1 - up, 2 - right, 3 - down, 4 - left
string create(const string& base,
              const int zidx,
              const int dir) {
  string r(base);
  switch (dir) {
    case 1: {
      swap(r[zidx], r[zidx-4]);
      break;
    }
    case 2: {
      swap(r[zidx], r[zidx+1]);
      break;
    }
    case 3: {
      swap(r[zidx], r[zidx+4]);
      break;
    }
    case 4: {
      swap(r[zidx], r[zidx-1]);
      break;
    }
  }
  return r;
}

int solve() {
  string s;
  for (int i = 0; i < 8; ++i)
    s += '0' + t[i];
  queue<P> Q;
  Q.push(P(s, 0));
  
  const string goal = "01234567";
  while (!Q.empty()) {
    P p = Q.front(); Q.pop();
    if (memo.count(p.first)) continue;
    memo.insert(p.first);
    // is end
    if (p.first == goal) {
      return p.second;
    }
    int zidx;
    const int nc = p.second + 1;
    for (int i = 0; i < 8; ++i)
      if (p.first[i] == '0') zidx = i;
    switch (zidx) {
      case 0: {
        Q.push(P(create(p.first, zidx, 2), nc));
        Q.push(P(create(p.first, zidx, 3), nc));
        break;
      }
      case 1:
      case 2: {
        Q.push(P(create(p.first, zidx, 2), nc));
        Q.push(P(create(p.first, zidx, 3), nc));
        Q.push(P(create(p.first, zidx, 4), nc));
        break;
      }
      case 3: {
        Q.push(P(create(p.first, zidx, 3), nc));
        Q.push(P(create(p.first, zidx, 4), nc));
        break;
      }
      case 4: {
        Q.push(P(create(p.first, zidx, 1), nc));
        Q.push(P(create(p.first, zidx, 2), nc));
        break;
      }
      case 5:
      case 6: {
        Q.push(P(create(p.first, zidx, 1), nc));
        Q.push(P(create(p.first, zidx, 2), nc));
        Q.push(P(create(p.first, zidx, 4), nc));        
        break;
      }
      case 7: {
        Q.push(P(create(p.first, zidx, 1), nc));
        Q.push(P(create(p.first, zidx, 4), nc));
        break;
      }
    }
  }
  return 0;
}

int main() {
  while (scanf("%d", t) != -1) {
    for (int i = 1; i < 8; ++i)
      scanf("%d", t+i);
    memo.clear();
    printf("%d\n", solve());
  }
  return 0;
}