#include #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; bool is_kadomatsu(int a,int b,int c){ if(a==b || a==c || b==c) return false; if(ac) return true; if(a>b && b> Q; Q.emplace(0,1,0); Q.emplace(1,0,3); while(!Q.empty()){ int y,x,k; tie(y,x,k)=Q.front(); Q.pop(); if(y==h-1 && x==w-1) return printf("%d\n",d[y][x][k]),0; int a0=a[y-dy[k]][x-dx[k]],a1=a[y][x]; rep(kk,4){ int yy=y+dy[kk],xx=x+dx[kk]; if(0<=yy && yy