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

#define N 4
#define N2 16
#define LIMIT 45
#define TRUE 1
#define FALSE 0

static const int dx[4] = {0, -1, 0, 1};
static const int dy[4] = {1, 0, -1, 0};
static const char dir[4] = {'r', 'u', 'l', 'd'};

int MDT[N2][N2];

typedef struct Puzzle {
  int board[N2];
  int space;
  int MD;
} Puzzle;

struct Puzzle state;
int limit;
int path[LIMIT];

int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int abs(int a) {
  if (a < 0) {
    return -a;
  }
  
  return a;
}

int swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int calculateManhattanDistance(Puzzle puzzle) {
  int sum = 0;

  for (int i = 0; i < N2; i++) {
    if (puzzle.board[i] == N2) {
      continue;
    }

    sum += MDT[i][puzzle.board[i] - 1];
  }

  return sum;
}

int doDepthFirstSearch(int depth, int prev) {
  if (state.MD == 0) {
    return TRUE;
  }

  if (depth + state.MD > limit) {
    return FALSE;
  }

  int sx = state.space / N;
  int sy = state.space % N;

  Puzzle tmp;

  for (int r = 0; r < N; r++) {
    int tx = sx + dx[r];
    int ty = sy + dy[r];

    if ((tx < 0) || (ty < 0) || (tx >= N) || (ty >= N)) {
      continue;
    }

    if(max(prev, r) - min(prev, r) == 2) {
      continue;
    }

    tmp = state;

    state.MD -= MDT[tx * N + ty][state.board[tx * N + ty] - 1];
    state.MD += MDT[sx * N + sy][state.board[tx * N + ty] - 1];

    swap(&state.board[tx * N + ty], &state.board[sx * N + sy]);

    state.space = tx * N + ty;

    if (doDepthFirstSearch(depth + 1, r)) {
      path[depth] = r;
      
      return TRUE;
    }

    state = tmp;
  }

  return FALSE;
}

int isSolved() {
  for (int i = 0; i < N2; i++) {
    if (state.board[i] != i + 1) {
      return FALSE;
    } 
  }

  return TRUE;
}

void doIterativeDpeepening(char *answer, Puzzle input) {
  input.MD = calculateManhattanDistance(input);

  for (limit = input.MD; limit <= LIMIT; limit++) {
    state = input;

    if (doDepthFirstSearch(0, -100)) {
      for (int i = 0; i < LIMIT; i++) {
	answer[i] = '*';
      }

      for (int i = 0; i < limit; i++) {
	answer[i] = dir[path[i]];
      }

      return;
    }
  }
}

void initManhattanDistanceTable() {
  for (int i = 0; i < N2; i++) {
    for (int j = 0; j < N2; j++) {
      MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N);
    }
  }
}

Puzzle readInitialBoard() {
  Puzzle input;
  
  for (int i = 0; i < N2; i++) {
    scanf("%d", &input.board[i]);

    if (input.board[i] == 0) {
      input.board[i] = N2;
      input.space = i;
    }
  }

  return input;
}

  /* for (int i = 0; i < N2; i++) { */
  /*   for (int j = 0; j < N2; j++) { */
  /*     printf("%02d ", MDT[i][j]); */
  /*   } */
  /*   printf("\n"); */
  /* } */

  /* return 0; */

int main(void) {
  initManhattanDistanceTable();

  Puzzle input = readInitialBoard();
  
  char answer[LIMIT];

  for (int i = 0; i < LIMIT; i++) {
    answer[i] = '*';
  }
  
  doIterativeDpeepening(answer, input);

  int count = 0;
  
  for (int i = 0; i < LIMIT; i++) {
    if (answer[i] == '*') {
      break;
    } else {
      count++;
    }
  }
  
  printf("%d\n", count);

  return 0;
}



Measured memory_bytes: 2128