import java.util.*; public class Main { static int INF=1000000; public static int min2(int a,int b){ return (adist[i-1][j]+1){ dist[i][j]=dist[i-1][j]+1; change=true; } }else{ if(dist[i][j]>dist[i-1][j]*2+2){ dist[i][j]=dist[i-1][j]*2+2; change=true; } } } if(i!=H-1){ if(map[i][j]==0){ if(dist[i][j]>dist[i+1][j]+1){ dist[i][j]=dist[i+1][j]+1; change=true; } }else{ if(dist[i][j]>dist[i+1][j]*2+2){ dist[i][j]=dist[i+1][j]*2+2; change=true; } } } if(j!=0){ if(map[i][j]==0){ if(dist[i][j]>dist[i][j-1]+1){ dist[i][j]=dist[i][j-1]+1; change=true; } }else{ if(dist[i][j]>dist[i][j-1]*2+2){ dist[i][j]=dist[i][j-1]*2+2; change=true; } } } if(j!=W-1){ if(map[i][j]==0){ if(dist[i][j]>dist[i][j+1]+1){ dist[i][j]=dist[i][j+1]+1; change=true; } }else{ if(dist[i][j]>dist[i][j+1]*2+2){ dist[i][j]=dist[i][j+1]*2+2; change=true; } } } } } if(!change){ break; } } System.out.println(dist[H-1][W-1]); } }