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

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

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

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

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

inline bool check() {
  for (int i = 0; i < 8; i++) {
    if (a[i] != i) { return false; }
  }
  return true;
}

int bfs(int src) {
  memset(vis, 0, sizeof(vis));
  queue<Node> que;
  que.push({src, 0});
  vis[src] = true;
  while (!que.empty()) {
    int x = que.front().x, step = que.front().step, t; que.pop();
    uhs(x);
    if (check()) { return step; }
    int i = 0;
    for (; a[i] != 0; i++);
    if (i >= 4) {
      swap(a[i], a[i - 4]); t = hs();
      if (!vis[t]) { vis[t] = true; que.push({t, step + 1}); }
      swap(a[i], a[i - 4]);
    }
    if (i < 4) {
      swap(a[i], a[i + 4]); t = hs();
      if (!vis[t]) { vis[t] = true; que.push({t, step + 1}); }
      swap(a[i], a[i + 4]);
    }
    if (i % 4 != 0) {
      swap(a[i], a[i - 1]); t = hs();
      if (!vis[t]) { vis[t] = true; que.push({t, step + 1}); }
      swap(a[i], a[i - 1]);
    }
    if (i % 4 != 3) {
      swap(a[i], a[i + 1]); t = hs();
      if (!vis[t]) { vis[t] = true; que.push({t, step + 1}); }
      swap(a[i], a[i + 1]);
    }
  }
  return -1;
}

int main() {
  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", bfs(hs()));
  }
}