#include<bits/stdc++.h> using namespace std;
typedef long long ll; #define re register
int x,y,cnt; int tmp[5][5]; int puz[16];
const int goal[5][5]={ {0,0,0,0,0}, {0,1,2,3,4}, {0,5,6,7,8}, {0,9,10,11,12}, {0,13,14,15,0} };
const int dx[4]={0,1,-1,0}; const int dy[4]={1,0,0,-1};
const int px[16]={4,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4}; const int py[16]={4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3};
inline int mabs(int a){ return a>0?a:-a; }
inline bool check(){ int cnt=0,tot=0; for (int i=1;i<=4;++i){ for (int j=1;j<=4;++j){ puz[tot]=tmp[i][j]; tot++; } } for (int i=0;i<16;++i){ if (puz[i]==0) cnt+=3-i/4; else{ for (int j=0;j<i;++j){ if (puz[j] && puz[j]>puz[i]){ cnt++; } } } } return !(cnt&1); }
inline int evaluate(){ int res=0; for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ if(tmp[i][j]==0)continue; res+=mabs(i-px[tmp[i][j]])+mabs(j-py[tmp[i][j]]); } } return res; }
inline int getway(int i){ if(dx[i]==-1)return 2; if(dx[i]==1)return 1; if(dy[i]==-1)return 4; if(dy[i]==1)return 3; }
inline bool test(int i,int pre){ if(dx[i]==-1&&pre==1)return true; if(dx[i]==1&&pre==2)return true; if(dy[i]==-1&&pre==3)return true; if(dy[i]==1&&pre==4)return true; return false; }
bool IDA_star(int step,int x,int y,int pre){ int eva=evaluate(); if(!eva)return true; if(step+eva>cnt)return false; for(int i=0;i<4;i++){ int xx=x+dx[i]; int yy=y+dy[i]; if(xx>4||xx<1||yy>4||yy<1||test(i,pre))continue; swap(tmp[xx][yy],tmp[x][y]); if(IDA_star(step+1,xx,yy,getway(i)))return true; swap(tmp[xx][yy],tmp[x][y]); } return false; }
inline int read(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();} return x*f; }
int main(){ #ifdef LawrenceSivan freopen("puzzle.in","r",stdin); freopen("puzzle.out","w",stdout); #endif for(re int i=1;i<=4;i++){ for(re int j=1;j<=4;j++){ tmp[i][j]=read(); if(tmp[i][j]==0)x=i,y=j; } }
if(!evaluate()){ printf("%d\n",0); return 0; } if(!check()){ printf("No\n"); return 0; } while(++cnt){ if(IDA_star(0,x,y,0)){ printf("%d\n",cnt); return 0; } if(cnt>50){ printf("No\n"); return 0; } } return 0; }
|