Problem: The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.

In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.


1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15


You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0


Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.

Constraints

* The given puzzle is solvable in at most 45 steps.

Input

The $4 \times 4$ integers denoting the pieces or space are given.

Output

Print the fewest steps in a line.

Example

Input

1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15


Output

8

Solution (in CPP):

Solution:
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define N 4
#define N2 16

struct puzzle{
  int x[N2];
  int space,MD,cost;
  bool operator < (const puzzle &p)const{
    for(int i=0;i<N2;i++){
      if(x[i]==p.x[i]) continue;
      return x[i]>p.x[i];
    }
    return false;
  }
};

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

struct state{
  puzzle puz;
  int est;
  bool operator < (const state &s)const{
    return est > s.est;
  }
};

int astar(puzzle);
int getallmd(puzzle);

int main(){
  puzzle in;

  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);
    }
  }
      
  for(int i=0;i<N2;i++){
    cin >> in.x[i];
    if(in.x[i] == 0){
      in.x[i] = N2;
      in.space = i;
    }
  }
  cout << astar(in) << endl;

  return 0;
}

int astar(puzzle p){
  priority_queue<state> PQ;
  map<puzzle,bool> V;
  puzzle m,k;
  state ini;
  p.MD=getallmd(p);
  p.cost = 0;
  ini.puz = p;
  ini.est = getallmd(p);
  PQ.push(ini);

  while(!PQ.empty()){
    state st=PQ.top(); PQ.pop();
    m=st.puz;
    
    if(m.MD == 0) return m.cost;
    V[m]=true;
    
    int px=m.space/N;
    int py=m.space%N;
    for(int r=0;r<4;r++){
      int tx=px+dx[r];
      int ty=py+dy[r];
      if(tx<0 || ty<0 || tx>=N || ty>=N) continue;
      k=m;

      k.MD -= MDT[tx*N+ty][k.x[tx*N+ty]-1];
      k.MD += MDT[px*N+py][k.x[tx*N+ty]-1];
      
      swap(k.x[px*N+py],k.x[tx*N+ty]);
      k.space=tx*N+ty;
      if(!V[k]){
	k.cost++;
	state newp;
	newp.puz=k;
	newp.est=k.cost+k.MD;
	PQ.push(newp);
      }
    }
  }
  return -1;
}

int getallmd(puzzle pz){
  int sum=0;
  for(int i=0;i<N2;i++){
    if(pz.x[i]==N2)continue;
    sum+=MDT[i][pz.x[i]-1];
  }
  return sum;
}