#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>
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 a[8], ar[2][4], ans;
const int dx[4] = { 0, 0, -1, 1 }, dy[4] = { 1, -1, 0, 0 };

bool check() {
  int ptr = 0;
  rep (i,2) {
    rep (j,4) {
     if (ar[i][j] != ptr) return false;
     ptr++;
    }
  }
  return true;
}

void dfs(int turn, int x, int y, int prev) {
  if (turn >= ans) return;
  if (check()) ans = turn;
  rep (i,4) {
    if ((prev == 0 && i == 1) || (prev == 1 && i == 0) ||
        (prev == 2 && i == 3) || (prev == 3 && i == 2)) { continue; }
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= 4 || ty >= 2) continue;
    swap(ar[y][x], ar[ty][tx]);
    dfs(turn + 1, tx, ty, i);
    swap(ar[y][x], ar[ty][tx]);
  }
}

void solve() {
  int x, y;
  rep (i,2) rep (j,4) if (!ar[i][j]) {
    y = i, x = j;
  }
  ans = 30;
  dfs(0, x, y, -1);
  cout << ans << endl;
}

int main() {
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    int ptr = 0;
    rep (i,2) {
      rep (j,4) {
        ar[i][j] = a[ptr++];
      }
    }
    solve();
  }
  return 0;
}