#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <cmath>
#include <set>

using namespace std;

using Weight=int;
using Encoded=int;

struct State {
    Weight hcost, acost;
    Encoded encoded;
    State() {}
    State(Weight h, Weight a, Encoded e): hcost(h), acost(a), encoded(e) {}
    bool operator>(const State & state2) const {
        if (hcost+acost != state2.hcost+state2.acost) {
            return hcost+acost > state2.hcost+state2.acost;
        } else {
            return acost > state2.acost;
        }
    }
};

const Weight INF=1<<29;

template <class T>
using lp_queue=priority_queue<T, deque<T>, greater<T>>;

Encoded encode(vector<int> d) {
    int cur=0;
    Encoded enc=0;
    for (size_t i=0; i<d.size()-1; ++i) {
        cur = d[i];
        for (size_t j=i+1; j<d.size(); ++j)
            if (cur < d[j])
                --d[j];

        (enc += d[i]) *= d.size()-i-1;
    }

    return enc;
}

vector<int> decode(Encoded e) {
    vector<int> d(9);
    for (size_t i=1; i<=9; ++i) {
        d[9-i] = e % i;
        e /= i;
    }

    for (size_t i=9; i--;)
        for (size_t j=i+1; j<9; ++j)
            if (d[i] <= d[j])
                ++d[j];

    return d;
}

Weight heucost(Encoded start, Encoded end) {
    static const vector<int> dend=decode(end);
    vector<int> dstart=decode(start);

    Weight h=0;
    for (size_t i=0; i<9; ++i)
        for (size_t j=0; j<9; ++j)
            if (dstart[i] == dend[j]) {
                h += abs(i/3-j/3)+abs(i%3-j%3);
                break;
            }

    return h;
}

vector<pair<Weight, Encoded>> pmoves(Encoded cur) {
    vector<int> d=decode(cur);
    size_t i;
    for (i=0; d[i]; ++i);

    vector<pair<Weight, Encoded>> p;
    if (i/3 < 2) {
        swap(d[i], d[i+3]);
        p.push_back(make_pair(1, encode(d)));
        swap(d[i], d[i+3]);
    }
    if (i/3 > 0) {
        swap(d[i], d[i-3]);
        p.push_back(make_pair(1, encode(d)));
        swap(d[i], d[i-3]);
    }
    if (i%3 < 2) {
        swap(d[i], d[i+1]);
        p.push_back(make_pair(1, encode(d)));
        swap(d[i], d[i+1]);
    }
    if (i%3 > 0) {
        swap(d[i], d[i-1]);
        p.push_back(make_pair(1, encode(d)));
        swap(d[i], d[i-1]);
    }
    return p;
}

Weight astar(Encoded start, Encoded end) {
    lp_queue<State> q;
    q.push(State(heucost(start, end), 0, start));

    Weight opt=INF;
    set<Encoded> visited;
    while (!q.empty()) {
        State p=q.top(); q.pop();
        Weight cur_cost=p.acost;
        Encoded cur_state=p.encoded;
        visited.insert(cur_state);

        if (opt < cur_cost) continue;
        if (cur_state == end)
            if (opt > cur_cost)
                opt = cur_cost;

        vector<pair<Weight, Encoded>> ms=pmoves(cur_state);
        for (pair<Weight, Encoded> m: ms) {
            if (visited.count(m.second)) continue;

            q.push(State(heucost(m.second, end), cur_cost+m.first, m.second));
        }
    }

    return opt;
}

int main() {
    vector<int> d(9);
    for (size_t i=0; i<9; ++i)
        scanf("%d", &d[i]);

    Encoded e=encode(d);

    Weight c=astar(e, 46233);
    printf("%d\n", c);
    return 0;
}

Measured memory_bytes: 11452