import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.TreeSet; public class Main { public static Queue qu = new LinkedList(); public static int[][][] dp; public static void main(String[] args) throws NumberFormatException, IOException{ ContestScanner in = new ContestScanner(); int w = in.nextInt(); int h = in.nextInt(); int[][] masu = new int[h][w]; for(int i=0; i= 0 && x < w && y >= 0 && y < h && d.isKadomatsuSequence(masu[y][x])){ if((dp[d.world][y][x] == -1 || dp[d.world][y][x] >= 0 && dp[d.world][y][x] > next)){ dp[d.world][y][x] = next; if(x == w-1 && y == h-1){ ok = true; break; } qu.add(new Data(x, y, next, d.a1, masu[y][x])); } } if(ok) break; } } if(dp[0][h-1][w-1] == -1 && dp[1][h-1][w-1] == -1){ System.out.println(-1); }else{ if(dp[0][h-1][w-1] < dp[1][h-1][w-1] && dp[0][h-1][w-1] != -1) System.out.println(dp[0][h-1][w-1]); else System.out.println(dp[1][h-1][w-1]); } } } class Data{ int turn; int x; int y; int a0; int a1; int world; public Data(int x, int y, int turn, int a0, int a1){ this.turn = turn; this.x = x; this.y = y; this.a0 = a0; this.a1 = a1; } public boolean isKadomatsuSequence(int a){ if(a0 == -1){ if(a1 < a) world = 1; return true; } if(a != a0 && a != a1 && a0 != a1){ if(a1 < a && a1 < a0){ world = 1; return true; }else if(a1 > a && a1 > a0){ world = 0; return true; } } return false; } } class MyComp implements Comparator{ public int compare(int[] a, int[] b) { return a[0] - b[0]; } } class Reverse implements Comparator{ public int compare(Integer arg0, Integer arg1) { return arg1 - arg0; } } class Node{ int id; List edge = new ArrayList(); public Node(int id){ this.id = id; } public void createEdge(Node node){ edge.add(node); } } class NodeW{ int id; List edge = new ArrayList(); List costList = new ArrayList(); public NodeW(int id) { this.id = id; } public void createEdge(NodeW node, int cost) { edge.add(node); costList.add(cost); } } class Range> implements Comparable>{ T start; T end; public Range(T start, T end){ this.start = start; this.end = end; } public boolean inRange(T val){ if(start.compareTo(val) <= 0 && end.compareTo(val) >= 0){ return true; } return false; } public boolean isCommon(Range range){ if(inRange(range.start) || inRange(range.end) || range.inRange(start)){ return true; } return false; } public Range connect(Range range){ if(!isCommon(range)) return null; Range res = new Range(start.compareTo(range.start) <= 0 ? start : range.start, end.compareTo(range.end) >= 0 ? end : range.end); return res; } public boolean connectToThis(Range range){ if(!isCommon(range)) return false; start = start.compareTo(range.start) <= 0 ? start : range.start; end = end.compareTo(range.end) >= 0 ? end : range.end; return true; } @Override public int compareTo(Range range) { int res = start.compareTo(range.start); if(res == 0) return end.compareTo(range.end); return res; } public String toString(){ return "["+start+","+end+"]"; } } class RangeSet>{ TreeSet> ranges = new TreeSet>(); public void add(Range range){ Range con = ranges.floor(range); if(con != null){ if(con.connectToThis(range)) range = con; } con = ranges.ceiling(range); while(con != null && range.connectToThis(con)){ ranges.remove(con); con = ranges.ceiling(range); } ranges.add(range); } public String toString(){ StringBuilder bld = new StringBuilder(); for(Range r: ranges){ bld.append(r+"\n"); } return bld.toString(); } } class MyMath{ public final static double PIhalf = Math.PI/2.0; public static double pAngle(double x, double y){ // ベクトル(1, 0)と(x, y)とのなす角を返す(rad:0 to 2pi) if(x == 0){ if(y == 0){ System.err.println("pAngle error: zero vector."); return 0; }else if(y < 0){ return PIhalf*3.0; }else{ return PIhalf; } } double rad = Math.atan(y/x); if(rad < 0){ rad += Math.PI*2.0; } return rad; } public static long fact(long n){ long res = 1; while(n > 0){ res *= n--; } return res; } public static long[][] pascalT(int n){ long[][] tri = new long[n][]; for(int i=0; i