//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
//using Int=int_fast64_t;
typedef long long int ll;
typedef pair<ll,ll>Pll;
typedef pair<int,int>Pin;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
static const int dx[4]={-1,0,1,0};
static const int dy[4]={0,-1,0,1};
static const char dir[4]={'u','l','d','r'};
ll cmp1(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}

//---------------------------------------------------------------------------
#define n 3
#define n2 9

struct puzzle{
    int f[n2];
    int space;
    string path;
    
    bool operator < (const puzzle &p)const{
        for(int i=0;i<n2;i++){
            if(f[i]==p.f[i])continue;
            return f[i]>p.f[i];
        }
        return false;
    }
};

bool istarget(puzzle p){
    for(int i=0;i<n2;i++){
        if(p.f[i]!=(i+1))return false;
    }
    return true;
}

string bfs(puzzle s){
    queue<puzzle>q;
    map<puzzle,bool>V;
    puzzle u,v;
    s.path="";
    q.push(s);
    V[s]=1;
    while(!q.empty()){
        u=q.front();q.pop();
        if(istarget(u))return u.path;
        int sx=u.space/n;
        int sy=u.space%n;
        for(int r=0;r<4;r++){
            int tx=sx+dx[r];
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=n||ty>=n)continue;
            v=u;
            swap(v.f[u.space],v.f[tx*n+ty]);
            v.space=tx*n+ty;
            if(!V[v]){
                V[v]=1;
                v.path+=dir[r];
                q.push(v);
            }
        }
    }
    return "unsolvable";
}
//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    puzzle in;
    for(int i=0;i<n2;i++){
        cin>>in.f[i];
        if(in.f[i]==0){
            in.f[i]=n2;
            in.space=i;
        }
    }
    string ans=bfs(in);
    cout<<ans.size()<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------



Measured memory_bytes: 30512