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

typedef long long ll;
const int N = 8;
const int M = 15;

int a[N];
int ans[1 << 24];
struct Node { int x, p, step; };

inline int hs() {
  int ret = 0;
  for (int i = 0; i < 8; i++) { ret = (ret << 3) + a[i]; }
  return ret;
}

inline void uhs(int x) {
  for (int i = 7; i >= 0; i--) { a[i] = x & 7; x >>= 3; }
}

void bfs() {
  memset(ans, -1, sizeof(ans));
  int src = 0;
  for (int i = 0; i < 8; i++) { src = (src << 3) + i; }
  queue<Node> que;
  que.push({src, 0, 0});
  ans[src] = 0;
  while (!que.empty()) {
    int x = que.front().x, p = que.front().p, step = que.front().step, t; que.pop();
    ans[x] = step;
    uhs(x);
    if (p >= 4) {
      swap(a[p], a[p - 4]); t = hs();
      if (ans[t] == -1) { que.push({t, p - 4, step + 1}); }
      swap(a[p], a[p - 4]);
    } else {
      swap(a[p], a[p + 4]); t = hs();
      if (ans[t] == -1) { que.push({t, p + 4, step + 1}); }
      swap(a[p], a[p + 4]);
    }
    if (p != 0 && p != 4) {
      swap(a[p], a[p - 1]); t = hs();
      if (ans[t] == -1) { que.push({t, p - 1, step + 1}); }
      swap(a[p], a[p - 1]);
    }
    if (p != 3 && p != 7) {
      swap(a[p], a[p + 1]); t = hs();
      if (ans[t] == -1) { que.push({t, p + 1, step + 1}); }
      swap(a[p], a[p + 1]);
    }
  }
}

int main() {
  bfs();
  while (~scanf("%d%d%d%d%d%d%d%d", a, a + 1, a + 2, a + 3, a + 4, a + 5, a + 6, a + 7)) {
    printf("%d\n", ans[hs()]);
  }
}