using System; using System.IO; using System.Text; using System.Linq; using System.Diagnostics; using System.Collections.Generic; using Enu = System.Linq.Enumerable; class Program { static readonly int[] dr = { 0, 0, -1, 1 }; static readonly int[] dc = { -1, 1, 0, 0 }; static readonly int INF = (int)1e9; int H, W; void Solve() { W = reader.Int(); H = reader.Int(); var grid = reader.IntGrid(H); var que = new Queue(); var minCost = new int[H, W, 10]; Fill(minCost, INF); minCost[0, 0, 0] = 0; que.Enqueue(new State(-1, grid[0][0], 0, 0, 0)); while (que.Count > 0) { var s = que.Dequeue(); if (s.r == H - 1 && s.c == W - 1) { Console.WriteLine(s.steps); return; } for (int d = 0; d < dr.Length; d++) { int nr = s.r + dr[d], nc = s.c + dc[d]; if (OK(s, nr, nc, grid)) { if (s.steps + 1 < minCost[nr, nc, s.curr]) { minCost[nr, nc, s.curr] = s.steps + 1; que.Enqueue(new State(s.curr, grid[nr][nc], nr, nc, s.steps + 1)); } } } } Console.WriteLine(-1); } private bool OK(State s, int nr, int nc, int[][] grid) { if (!IsInside(nr, nc)) return false; int next = grid[nr][nc]; if (s.prev == next || s.curr == next) return false; if (s.prev < 0) return true; int mid = new[] { s.prev, s.curr, next }.OrderBy(e => e).ElementAt(1); return mid == s.prev || mid == next; } bool IsInside(int r, int c) { return r >= 0 && r < H && c >= 0 && c < W; } struct State { public short prev, curr, r, c; public int steps; public State(int p2, int p1, int r, int c, int step) { prev = (short)p2; curr = (short)p1; this.r = (short)r; this.c = (short)c; steps = step; } } void Fill(T[, ,] a, T val) { for (int i = 0; i < a.GetLength(0); i++) for (int j = 0; j < a.GetLength(1); j++) for (int k = 0; k < a.GetLength(2); k++) a[i, j, k] = val; } static void Main() { new Program().Solve(); } Reader reader = new Reader(Console.In); class Reader { private readonly TextReader reader; private readonly char[] separator = { ' ' }; private readonly StringSplitOptions removeOp = StringSplitOptions.RemoveEmptyEntries; private string[] A = new string[0]; private int i; public Reader(TextReader r) { reader = r; } public bool HasNext() { return Enqueue(); } public string String() { return Dequeue(); } public int Int() { return int.Parse(Dequeue()); } public long Long() { return long.Parse(Dequeue()); } public double Double() { return double.Parse(Dequeue()); } public int[] IntLine() { var s = Line(); return s == "" ? new int[0] : Array.ConvertAll(Split(s), int.Parse); } public int[] IntArray(int N) { return Enumerable.Range(0, N).Select(i => Int()).ToArray(); } public int[][] IntGrid(int H) { return Enumerable.Range(0, H).Select(i => IntLine()).ToArray(); } public string[] StringArray(int N) { return Enumerable.Range(0, N).Select(i => Line()).ToArray(); } public string Line() { return reader.ReadLine().Trim(); } private string[] Split(string s) { return s.Split(separator, removeOp); } private bool Enqueue() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return Enqueue(); A = Split(line); i = 0; return true; } private string Dequeue() { Enqueue(); return A[i++]; } } }