#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e6+5,MAXB=2e7;
#define ll long long
struct node {
int x[10],step;
} a;
map<int,int>m;
int num(int a[]) {
int ans=0;
for(int i=0; i<8; i++)
ans=ans*10+a[i];
return ans;
}
void bfs(node a) {
int pos,r=123804765,n,f[4]= {-3,3,-1,1};
queue<node>q;
q.push(a);
while(!q.empty()) {
a=q.front(),q.pop();
for(int i=0; i<8; i++)
if(a.x[i]==0) {
pos=i;
break;
}
for(int i=0; i<4; i++) {
if((i==0&&pos<2)||(i==1&&pos<6)||(i==2&&pos%3!=0)||(i==3&&pos%3!=2)) {
swap(a.x[pos],a.x[pos+f[i]]);
n=num(a.x);
if(n==r) {
cout<<a.step+1;
return;
}
if(m[n]==0) {
m[n]=++a.step;
q.push(a);
a.step--;
}
swap(a.x[pos],a.x[pos+f[i]]);
}
}
}
}
void zuana() {
int n,pos=8;
node a;
cin>>n;
if(n==123804765) {
cout<<0;
return;
}
m[n]=-1;
a.x[0]=0;
while(n) {
a.x[pos--]=n%10;
n/=10;
}
a.step=0;
bfs(a);
}
int main() {
zuana();
return 0;
}