
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.array;

struct State {
  string board;
  int pos;
  int counter;
}

void solve(ref int[string] hash) {
  auto directions = [1, -1, 4, -4];
  State[] queue;
  string init = "01234567";
  hash[init] = 0;
  queue ~= State(init, 0, 0);
  while (!queue.empty) {
    State state = queue.front;
    queue.popFront;
    foreach(int i, int d; directions) {
      int new_pos = state.pos + d;
      if (new_pos < init.length && new_pos >= 0 &&
          !(new_pos == 3 && i == 0) && !(new_pos == 4 && i == 1)) {
        char[] new_board = state.board.dup;
        swap(new_board[state.pos], new_board[new_pos]);
        string str = to!string(new_board);
        if (hash.get(str, -1) == -1) {
          State s;
          s.board = str;
          s.pos = new_pos;
          s.counter = state.counter + 1;
          hash[str] = s.counter;
          queue ~= s;
        }
      }
    }
  }
}

void main() {
  string line;
  int[string] hash;
  solve(hash);
  while ((line = readln()).length != 0) {
    writeln(hash[line.strip.chomp.removechars(" ")]);
  }
}