#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define size 3
#define size2 9
#define size3 100000
#define max 36

typedef struct {
  char b[size][size],m,cost;
  int  space[2];
} Pz;

int head=0, tail=0;
Pz q[size3];

void enq(Pz pz){
  q[tail]=pz;
  tail = (tail+1)%size3;
}

Pz deq(void){
  int t;
  
  t = head;
  head = (head+1)%size3;

  return q[t];
}

int check(Pz pz){
  int i,j;

  pz.b[pz.space[0]][pz.space[1]] = size2;
  
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
      if(pz.b[i][j] != i*size + j + 1){
	return 0;
      }
    }
  }

  return 1;
}

int distance(Pz pz){
  int i,j,sum=0,t   ;

  /*for(i=0;i<size;i++){
    for(j=0;j<size;j++){
      printf("%d ",pz.b[i][j]);
    }
    printf("\n");
  }*/
  
  pz.b[pz.space[0]][pz.space[1]] = size2;
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
      t=pz.b[i][j]-1;
      sum += abs(i - t/size) + abs(j - t%size);
      //printf("%d ",tt);
    }
  }

  //printf("sum %d\n",sum);
  return sum;
}
int main(){
  int i,j,tate,yoko,dis=0,count=0;
  char tmpc;
  Pz inisial, tmpp, tmpp2;

  //nyu ryoku
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
      scanf("%d",&inisial.b[i][j]);
      if(inisial.b[i][j]==0){
	inisial.space[0] = i;
	inisial.space[1] = j;
      }
    }
  }
  inisial.cost = 0;
  inisial.m = '0';

  enq(inisial);

  //printf("%d\n",distance(inisial));
  
  if(check(inisial)!=1){
    while(head!=tail){
      tmpp2 = tmpp = deq();
      tate = tmpp.space[0];
      yoko = tmpp.space[1];

      //printf("\n");
      //printf("%d %d\n",head,tail);
      /*for(i=0;i<size;i++){
	for(j=0;j<size;j++){
	  printf("%d ",tmpp.b[i][j]);
	}
	printf("\n");
      }
      //printf("\n");
      
      //count++;
      //*/
      if(tate != 0 && tmpp.m != '1'){
	tmpc = tmpp2.b[tate-1][yoko];
	tmpp2.b[tate-1][yoko] = tmpp2.b[tate][yoko];
	tmpp2.b[tate][yoko] = tmpc;
	tmpp2.space[0] = tmpp2.space[0] - 1;
	tmpp2.cost++;
	if(check(tmpp2) == 1){
	  dis = tmpp2.cost;
	  break;
	}
	if(tmpp2.cost + distance(tmpp2) < max){
	  tmpp2.m = '2';
	  enq(tmpp2);
	}
      }
      tmpp2=tmpp;

      if(tate < size-1 && tmpp.m != '2'){
	tmpc = tmpp2.b[tate+1][yoko];
	tmpp2.b[tate+1][yoko] = tmpp2.b[tate][yoko];
	tmpp2.b[tate][yoko] = tmpc;
	tmpp2.space[0] = tmpp2.space[0] + 1;
	tmpp2.cost++;
	if(check(tmpp2) == 1){
	  dis = tmpp2.cost;
	  break;
	}
	if(tmpp2.cost + distance(tmpp2) < max){
	  tmpp2.m = '1';
	  enq(tmpp2);
	}
      }
      tmpp2=tmpp;

      if(yoko != 0 && tmpp.m != '3'){
	tmpc = tmpp2.b[tate][yoko-1];
	tmpp2.b[tate][yoko-1] = tmpp2.b[tate][yoko];
	tmpp2.b[tate][yoko] = tmpc;
	tmpp2.space[1] = tmpp2.space[1] - 1;
	tmpp2.cost++;
	if(check(tmpp2) == 1){
	  dis = tmpp2.cost;
	  break;
	}
	if(tmpp2.cost + distance(tmpp2) < max){
	  tmpp2.m = '4';
	  enq(tmpp2);
	}
      }
      tmpp2=tmpp;

      if(yoko < size-1 && tmpp.m != '4'){
	tmpc = tmpp2.b[tate][yoko+1];
	tmpp2.b[tate][yoko+1] = tmpp2.b[tate][yoko];
	tmpp2.b[tate][yoko] = tmpc;
	tmpp2.space[1] = tmpp2.space[1] + 1;
	tmpp2.cost++;
	if(check(tmpp2) == 1){
	  dis = tmpp2.cost;
	  break;
	}
	if(tmpp2.cost + distance(tmpp2) < max){
	  tmpp2.m = '3';
	  enq(tmpp2);
	}
      }
    }
  }

  printf("%d\n",dis);

  return 0;
}



Measured memory_bytes: 3816