#include<iostream>
#include<string>
#include<map>
#include<queue>
using namespace std;
map<string,bool> vis;
struct zt
{
string str;
long long ans;
};
long long cz[10] = {0,-1,+1,-3,+3};
int main()
{
string st;
cin >> st;
if(st == "123804765")
{
cout << 0;
return 0;
}
queue<zt> q;
q.push({st,0});
while(! q.empty())
{
zt head = q.front();
q.pop();
long long k;
for(long long i=0;i<head.str.size();i++)
{
if(head.str[i] == '0')
{
k = i;
break;
}
}
for(long long i=1;i<=4;i++)
{
long long f = k-cz[i];
if(f >= 0 && f < 9)
{
string d = head.str;
swap(d[f],d[k]);
if(vis.find(d) == vis.end())
{
q.push({d,head.ans+1});
vis[d] = 1;
if(d == "123804765")
{
cout << head.ans+1;
return 0;
}
}
}
}
}
return 0;
}