#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

using board = vector<int>;
 
int h, w, n;
constexpr int dx[4] = {0, 1, 0, -1};
constexpr int dy[4] = {1, 0, -1, 0};
 
struct state{
    int score, dist;
    board b;
 
    state(int s, int d, board b): score(s), dist(d), b(b){}
};
 
int xy_to_id(int x, int y){
    return x*w + y;
}
 
pair<int, int> id_to_xy(int i){
    return {i/w, i%w};
}
 
pair<int, int> blank(board &b){
    for(int i=0; i<n; ++i){
        if(b[i] == n-1) return id_to_xy(i);
    }
}
 
bool check(board &b){
    for(int i=0; i<n; ++i){
        if(b[i] != i) return false;
    }
    return true;
}
 
int heuristic_func(board &b){
    int ret = 0;
    for(int i=0; i<n; ++i)if(b[i] != n-1) ret += (b[i] != i);
    return ret;
}
 
int main(){
    h = w = 3;
    n = h * w;
 
    board b(n);
    for(int i=0; i<n; ++i){
        int c;
        cin >> c;
        b[i] = (c == 0 ? n-1 : c-1);
    }
 
    auto cmp = [](const state &s1, const state &s2){
        if(s1.score == s2.score){
            if(s1.dist == s2.dist) return s1.b < s2.b;
            return s1.dist < s2.dist;
        }
        return s1.score < s2.score;
    };
    map<board, int> min_score;
    set<state, decltype(cmp)> open_list(cmp);
    state init_state = {heuristic_func(b), 0, b};
    open_list.insert(init_state);
    min_score[init_state.b] = init_state.score;
 
    while(!open_list.empty()){
        auto cur_state = *open_list.begin();
        open_list.erase(cur_state);
        if(cur_state.score > 45 ||  cur_state.score > min_score[cur_state.b]) continue;
 
        auto bxy = blank(cur_state.b);
        for(int i=0; i<4; ++i){
            int nx = bxy.first + dx[i], ny = bxy.second + dy[i];
            if(0<=nx && nx<h && 0<=ny && ny<w){
                auto nxt_state = cur_state;
                swap(nxt_state.b[xy_to_id(nx, ny)], nxt_state.b[xy_to_id(bxy.first, bxy.second)]);
                nxt_state.dist = cur_state.dist + 1;
                nxt_state.score = nxt_state.dist + heuristic_func(nxt_state.b);
                if(nxt_state.score > 45) continue;
                if(min_score.find(nxt_state.b) == min_score.end() || min_score[nxt_state.b] > nxt_state.score){
                    min_score[nxt_state.b] = nxt_state.score;
                    open_list.insert(nxt_state);
                }
            }
        }
    }
    
    board cur_board(n);
    for(int i=0; i<n; ++i) cur_board[i] = i;
    cout << min_score[cur_board] << endl;
}


Measured memory_bytes: 26448