#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;

class board {
  public:
    int a[8];
    int z;
    int pz;
    int s;
    board() : z(0), pz(0), s(0) { memset(a, 0, sizeof(int)*8); }
    board(int *_a, int _z, int _pz, int _s) : z(_z), pz(_pz), s(_s) { memcpy(a, _a, sizeof(int)*8); }
};

int p2a(int *p) {
  int r = 0;
  for (int i=7; i>=0; i--) {
    r <<= 4;
    r += p[i];
  }
  return r;
}

void a2p(int a, int *p) {
  int t = a;
  for (int i=0; i<8; i++) {
    p[i] = t & 0xF;
    t >>= 4;
  }
}

int main() {
  int p[8];
  int d[8][3] = {
    {1, 4, 0},
    {-1, 1, 4},
    {-1, 1, 4},
    {-1, 4, 0},
    {-4, 1, 0},
    {-4, -1, 1},
    {-4, -1, 1},
    {-4, -1, 0}
  };
  queue<board> q;
  set<int> memory;
  board b;
  bool flg;
  int tmp, z;

  while (scanf("%d %d %d %d %d %d %d %d", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7]) == 8) {
    for (int i=0; i<8; i++) {
      if (!p[i]) {
        z = i;
        break;
      }
    }
    q.push(board(p, z, z, 0));
    memory.insert(p2a(p));
    while (!q.empty()) {
      b = q.front(); q.pop();
      flg = true;
      for (int i=0; i<8; i++) {
        if (b.a[i]!=i) {
          flg = false;
          break;
        }
      }
      if (flg) break;

      for (int i=0; i<3; i++) {
        if (d[b.z][i]) {
          z = b.z + d[b.z][i];
          if (z == b.pz) continue;
          if (z>=0 && z<8) {
            memcpy(p, b.a, sizeof(int)*8);
            tmp = p[b.z];
            p[b.z] = p[z];
            p[z] = tmp;
            tmp = p2a(p);
            if (memory.find(tmp) == memory.end()) {
              memory.insert(tmp);
              q.push(board(p, z, b.z, b.s+1));
            }
          }
        }
      }
    }
    printf("%d\n", b.s);
    while (!q.empty()) q.pop();
    memory.clear();
  }

  return 0;
}