#include //typedef //-------------------------#include const double pi = 3.141592653589793238462643383279; using namespace std; //conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } //typedef //------------------------------------------ typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef pair PLL; typedef pair TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector VLL; typedef vector VVLL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define SQ(a) ((a)*(a)) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(i,s,n) for(int i=s;i<(int)n;++i) #define REP(i,n) FOR(i,0,n) #define MOD 1000000007 #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; const double EPS = 1E-8; #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) class UnionFind { public: vector par; vector siz; UnionFind(int sz_): par(sz_), siz(sz_, 1) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(int sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } int root(int x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return siz[root(x)]; } }; ll modPow(ll x, ll n, ll mod = MOD){ if(n <= 0) return 1; ll res = 1; while(n){ if(n&1) res = (res * x)%mod; res %= mod; x = x * x %mod; n >>= 1; } return res; } #define SIEVE_SIZE 5000000+10 bool sieve[SIEVE_SIZE]; void makeSieve(){ for(int i=0; i tmp{x1,x2,x3}; sort(all(tmp)); if(tmp[0] != tmp[1] && tmp[1] != tmp[2] && tmp[0] != tmp[2]){ if(tmp[1] == x1 || tmp[1] == x3){ return true; } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int W, H; cin >> W >> H; vector> M(H, vector(W)); REP(i,H)REP(j,W) cin >> M[i][j]; REP(i,H) REP(j,W)REP(k,10)REP(l,10) dp[i][j][k][l] = INT_MAX; dp[0][0][M[0][0]][0] = 0; queue>>> q; q.push(make_pair(0,make_pair(0, make_pair(M[0][0], 0)))); while(q.size()){ pair>> p = q.front(); q.pop(); int d = p.first; int y = p.second.first/W, x = p.second.first%W; int n1 = p.second.second.first, n2 = p.second.second.second; if(y == H-1 && x == W-1) break; if(n2 == 0){ for(int i=0; i<4; i++){ int ny = y + dxy[i], nx = x + dxy[i+1]; if(ny >= 0 && ny < H && nx >= 0 && nx < W){ int num = M[ny][nx]; if(dp[ny][nx][num][n1] > dp[y][x][n1][n2] + 1){ dp[ny][nx][num][n1] = dp[y][x][n1][n2] + 1; q.push(make_pair(dp[ny][nx][num][n1], make_pair(ny*W+nx, make_pair(M[ny][nx], n1)))); } } } }else{ for(int i=0; i<4; i++){ int ny = y + dxy[i], nx = x + dxy[i+1]; if(ny >= 0 && ny < H && nx >= 0 && nx < W){ int num = M[ny][nx]; if(dp[ny][nx][num][n1] > dp[y][x][n1][n2] + 1 && isKadomatsu(n2, n1, M[ny][nx])){ dp[ny][nx][num][n1] = dp[y][x][n1][n2] + 1; q.push(make_pair(dp[ny][nx][num][n1], make_pair(ny*W+nx, make_pair(M[ny][nx], n1)))); } } } } } int ans = INT_MAX; for(int i=1; i<10; i++){ for(int j=1; j<10; j++){ ans = min(ans, dp[H-1][W-1][i][j]); } } if(ans == INT_MAX) ans = -1; cout << ans << endl; return 0; }