#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define tn int
#define thaonguyen( i, a, b ) for( tn i = a; i <= b; i++ )
#define nguyenthao( i, a, b ) for( tn i = a; i >= b; i-- )
#define thaonguyenxinh ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
char chr;
tn cost;
string s, u;
queue< pair< string, tn > > q;
unordered_set< string > st;
void move( tn position, tn type ){
if ( type == 1 ){
if ( position != 2 and position != 5 and position != 8 ){
swap( u[position], u[position+1] );
if ( st.find( u ) == st.end() ){
st.insert(u);
q.push( { u, cost + 1 } );
}
swap( u[position], u[position+1] );
}
}
if ( type == 2 ){
if ( position != 0 and position != 3 and position != 6 ){
swap( u[position], u[position-1] );
if ( st.find( u ) == st.end() ){
st.insert(u);
q.push( { u, cost + 1 } );
}
swap( u[position], u[position-1] );
}
}
if ( type == 3 ){
if ( position != 6 and position != 7 and position != 8 ){
swap( u[position], u[position+3] );
if ( st.find( u ) == st.end() ){
st.insert(u);
q.push( { u, cost + 1 } );
}
swap( u[position], u[position+3] );
}
}
if ( type == 4 ){
if ( position != 0 and position != 1 and position != 2 ){
swap( u[position], u[position - 3] );
if ( st.find( u ) == st.end() ){
st.insert(u);
q.push( { u, cost + 1 } );
}
swap( u[position], u[position - 3] );
}
}
}
void cfs(){
q.push( { s , 0 } );
while ( !q.empty() ){
u = q.front().fi;
cost = q.front().se;
if ( u == "123456789" ){
cout << q.front().se;
break;
}
q.pop();
thaonguyen( i, 0, s.size() - 1 )
thaonguyen( j, 1, 4 ){
move( i, j );
}
}
}
signed main(){
thaonguyenxinh
thaonguyen( i, 1, 9 )
cin >> chr,
s += chr;
st.insert(s);
cfs();
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4gCnVzaW5nIG5hbWVzcGFjZSBzdGQ7IAojZGVmaW5lIGVuZGwgIlxuIiAKI2RlZmluZSBmaSBmaXJzdCAKI2RlZmluZSBzZSBzZWNvbmQgCiNkZWZpbmUgdG4gaW50CiNkZW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uZW5kKCkgKXsKICAgICAgICAgICAgICAgICAgICBzdC5pbnNlcnQodSk7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKCB7IHUsIGNvc3QgKyAxIH0gKTsKICAgICAgICAgICAgICAgIH0gCiAgICAgICAgICAgc3dhcCggdVtwb3NpdGlvbl0sIHVbcG9zaXRpb24tMV0gKTsKICAgICAgICB9CiAgICB9CiAgICBpZiAoIHR5cGUgPT0gMyApewogICAgICAgIGlmICggcG9zaXRpb24gIT0gNiBhbmQgcG9zaXRpb24gIT0gNyBhbmQgcG9zaXRpb24gIT0gOCApewogICAgICAgICAgIHN3YXAoIHVbcG9zaXRpb25dLCB1W3Bvc2l0aW9uKzNdICk7CiAgICAgICAgICAgIGlmICggc3QuZmluZCggdSApID09IHN0LmVuZCgpICl7CiAgICAgICAgICAgICAgICAgICAgc3QuaW5zZXJ0KHUpOwogICAgICAgICAgICAgICAgICAgIHEucHVzaCggeyB1LCBjb3N0ICsgMSB9ICk7CiAgICAgICAgICAgICAgICB9IAogICAgICAgICAgICBzd2FwKCB1W3Bvc2l0aW9uXSwgdVtwb3NpdGlvbiszXSApOwogICAgICAgIH0KICAgIH0KICAgIGlmICggdHlwZSA9PSA0ICl7CiAgICAgICAgaWYgKCBwb3NpdGlvbiAhPSAwIGFuZCBwb3NpdGlvbiAhPSAxIGFuZCBwb3NpdGlvbiAhPSAyICl7CiAgICAgICAgICAgIHN3YXAoIHVbcG9zaXRpb25dLCB1W3Bvc2l0aW9uIC0gM10gKTsKICAgICAgICAgICAgaWYgKCBzdC5maW5kKCB1ICkgPT0gc3QuZW5kKCkgKXsKICAgICAgICAgICAgICAgICAgICBzdC5pbnNlcnQodSk7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKCB7IHUsIGNvc3QgKyAxIH0gKTsKICAgICAgICAgICAgICAgIH0gCiAgICAgICAgICAgIHN3YXAoIHVbcG9zaXRpb25dLCB1W3Bvc2l0aW9uIC0gM10gKTsKICAgICAgICB9CiAgICB9Cn0Kdm9pZCBjZnMoKXsKICAgIHEucHVzaCggeyBzICwgMCB9ICk7CiAgICB3aGlsZSAoICFxLmVtcHR5KCkgKXsKICAgICAgICB1ID0gcS5mcm9udCgpLmZpOyAKICAgICAgICBjb3N0ID0gcS5mcm9udCgpLnNlOwogICAgICAgIGlmICggdSA9PSAiMTIzNDU2Nzg5IiApewogICAgICAgICAgICBjb3V0IDw8IHEuZnJvbnQoKS5zZTsgCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBxLnBvcCgpOyAKICAgICAgICB0aGFvbmd1eWVuKCBpLCAwLCBzLnNpemUoKSAtIDEgKQogICAgICAgICAgICB0aGFvbmd1eWVuKCBqLCAxLCA0ICl7CiAgICAgICAgICAgICAgICBtb3ZlKCBpLCBqICk7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgfQp9CnNpZ25lZCBtYWluKCl7CiAgICB0aGFvbmd1eWVueGluaAogICAgdGhhb25ndXllbiggaSwgMSwgOSApCiAgICAgICAgY2luID4+IGNociwgCiAgICAgICAgcyArPSBjaHI7CiAgICBzdC5pbnNlcnQocyk7CiAgICBjZnMoKTsKfQ==