#include<bits/stdc++.h>
using namespace std;
int jc[8]={1};
struct st
{
string str;
int pos,step;
};
queue<st> q;
bool b[40321][10];
int hash(string s)
{
int ah,bh[8]={0,0,0,0,0,0,0,0};
for(int i=0;i<7;i++)
{
for(int j=i+1;j<8;j++)
{
if(s[j]<s[i]) bh[i]++;
}
}
for(int i=0;i<8;i++) ah+=jc[7-i]*bh[i];
return ah;
}
st Create(string sss,int _pos,int _step)
{
st tmp;
tmp.str=sss;
tmp.pos=_pos;
tmp.step=_step;
return tmp;
}
string cut(string _s,long long que)
{
string ans="";
for(int i=0;i<8;i++) ans+=_s[(que/int(pow(10,7-i)))%10];
return ans;
}
int main()
{
for(int i=1;i<8;i++) jc[i]=jc[i-1]*i;
string ss,n;
cin>>ss;
st sta;
sta.pos=4,sta.step=0,sta.str="";
for(int i=0;i<9;i++) if(i!=4) sta.str+=ss[i];
q.push(sta);
b[hash(sta.str)][4]=1;
while(!q.empty())
{
st now=q.front();
q.pop();
// cout<<now.str<<' '<<now.pos<<' '<<now.step<<endl;
if(now.str=="12384765"&&now.pos==4)
{
printf("%d",now.step);
return 0;
}
if(now.pos==0)
{
n=now.str;
if(!b[hash(n)][1])
{
b[hash(n)][1]=1;
q.push(Create(n,1,now.step+1));
}
n=cut(n,20134567);
if(!b[hash(n)][3])
{
b[hash(n)][3]=1;
q.push(Create(n,3,now.step+1));
}
}
else if(now.pos==1)
{
n=now.str;
if(!b[hash(n)][0])
{
b[hash(n)][0]=1;
q.push(Create(n,0,now.step+1));
}
if(!b[hash(n)][2])
{
b[hash(n)][2]=1;
q.push(Create(n,2,now.step+1));
}
n=cut(n,3124567);
if(!b[hash(n)][4])
{
b[hash(n)][4]=1;
q.push(Create(n,4,now.step+1));
}
}
else if(now.pos==2)
{
n=now.str;
if(!b[hash(n)][1])
{
b[hash(n)][1]=1;
q.push(Create(n,1,now.step+1));
}
n=cut(n,1423567);
if(!b[hash(n)][5])
{
b[hash(n)][5]=1;
q.push(Create(n,5,now.step+1));
}
}
else if(now.pos==3)
{
n=now.str;
if(!b[hash(n)][4])
{
b[hash(n)][4]=1;
q.push(Create(n,4,now.step+1));
}
n=cut(n,12034567);
if(!b[hash(n)][0])
{
b[hash(n)][0]=1;
q.push(Create(n,0,now.step+1));
}
n=now.str;
n=cut(n,1253467);
if(!b[hash(n)][6])
{
b[hash(n)][6]=1;
q.push(Create(n,6,now.step+1));
}
}
else if(now.pos==4)
{
n=now.str;
if(!b[hash(n)][3])
{
b[hash(n)][3]=1;
q.push(Create(n,3,now.step+1));
}
if(!b[hash(n)][5])
{
b[hash(n)][5]=1;
q.push(Create(n,5,now.step+1));
}
n=cut(n,2314567);
if(!b[hash(n)][1])
{
b[hash(n)][1]=1;
q.push(Create(n,1,now.step+1));
}
n=now.str;
n=cut(n,1236457);
if(!b[hash(n)][7])
{
b[hash(n)][7]=1;
q.push(Create(n,7,now.step+1));
}
}
else if(now.pos==5)
{
n=now.str;
if(!b[hash(n)][4])
{
b[hash(n)][4]=1;
q.push(Create(n,4,now.step+1));
}
n=cut(n,1342567);
if(!b[hash(n)][2])
{
b[hash(n)][2]=1;
q.push(Create(n,2,now.step+1));
}
n=now.str;
n=cut(n,1234756);
if(!b[hash(n)][8])
{
b[hash(n)][8]=1;
q.push(Create(n,8,now.step+1));
}
}
else if(now.pos==6)
{
n=now.str;
if(!b[hash(n)][7])
{
b[hash(n)][7]=1;
q.push(Create(n,7,now.step+1));
}
n=cut(n,1245367);
if(!b[hash(n)][3])
{
b[hash(n)][3]=1;
q.push(Create(n,3,now.step+1));
}
}
else if(now.pos==7)
{
n=now.str;
if(!b[hash(n)][6])
{
b[hash(n)][6]=1;
q.push(Create(n,6,now.step+1));
}
if(!b[hash(n)][8])
{
b[hash(n)][8]=1;
q.push(Create(n,8,now.step+1));
}
n=cut(n,1235647);
if(!b[hash(n)][4])
{
b[hash(n)][4]=1;
q.push(Create(n,4,now.step+1));
}
}
else
{
n=now.str;
if(!b[hash(n)][7])
{
b[hash(n)][7]=1;
q.push(Create(n,7,now.step+1));
}
n=cut(n,1234675);
if(!b[hash(n)][5])
{
b[hash(n)][5]=1;
q.push(Create(n,5,now.step+1));
}
}
}
return 0;
}
屎山代码见谅(