#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int result;
int dp[16777216];

string exc (string s, int a, int b) {
  char tmp = s[a];
  s[a] = s[b];
  s[b] = tmp;
  return s;
}

//dir...(0,1,2,3) = (???,???,???,???)
void recursive (int x, int y, int dir, int count, string current) {
  int k=1,dpid = 0;
  for(int i = 0; i < 8; ++i, k*=8) {
    dpid += k * (current[i] - '0');
  }
  if(count >= dp[dpid] || count >= result) {
    return;
  } else {
    dp[dpid] = count;
  }
  if(current == "01234567") {
    result = min(count, result);
    return;
  }
  count++;
  int id = x + y * 4;
  if(dir != 0 && x >= 1)recursive(x-1, y, 2, count, exc(current,id,id-1));
  if(dir != 1 && y == 1)recursive(x, y-1, 3, count, exc(current,id,id-4));
  if(dir != 2 && x <= 2)recursive(x+1, y, 0, count, exc(current,id,id+1));
  if(dir != 3 && y == 0)recursive(x, y+1, 1, count, exc(current,id,id+4));
}

int main () {
  string s;
  while(cin>>s) {
    int zero = s.find('0');
    int zero_x = zero % 4;
    int zero_y = zero / 4;
    result = 1e9;
    for(int i = 0; i < 16777216; ++i)
      dp[i] = 1e9;
    recursive(zero_x, zero_y, -1, 0, s);
    cout<<result<<endl;
  }
}