#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) for (int i=0;i<(n);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define inf 0x3f3f3f3f #define INF INT_MAX/3 #define PB push_back #define MP make_pair #define ALL(a) (a).begin(),(a).end() #define SET(a,c) memset(a,c,sizeof a) #define CLR(a) memset(a,0,sizeof a) #define pii pair #define pcc pair #define pic pair #define pci pair #define VS vector #define VI vector #define DEBUG(x) cout<<#x<<": "<b?b:a) #define MAX(a,b) (a>b?a:b) #define pi 2*acos(0.0) #define INFILE() freopen("in0.txt","r",stdin) #define OUTFILE()freopen("out0.txt","w",stdout) #define ll long long #define ull unsigned long long #define eps 1e-14 #define FST first #define SEC second #define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15) namespace { struct input_returnner { int N; input_returnner(int N_ = 0) :N(N_) {} template operator vector() const { vector res(N); for (auto &a : res) cin >> a; return std::move(res); } template operator T() const { T res; cin >> res; return res; } template T operator - (T right) { return T(input_returnner()) - right; } template T operator + (T right) { return T(input_returnner()) + right; } template T operator * (T right) { return T(input_returnner()) * right; } template T operator / (T right) { return T(input_returnner()) / right; } template T operator << (T right) { return T(input_returnner()) << right; } template T operator >> (T right) { return T(input_returnner()) >> right; } }; template input_returnner in() { return in(); } input_returnner in() { return input_returnner(); } input_returnner in(int N) { return std::move(input_returnner(N)); } } void solve(); /// ---template--- signed main(void) { SETUP; solve(); return 0; } struct Point { int x; int y; Point(int x_, int y_):x(x_),y(y_){} Point() {} }; bool operator < (list left, list right) { return left.size() < right.size(); } int dx[] = {1,0,-1,0}; int dy[] = {0,-1,0,1}; bool isKadomatsu(const list& points, const vector >& M, const Point& np) { if (points.size() < 2) return true; array cost; cost[0] = M[np.y][np.x]; auto it = points.rbegin(); cost[1] = M[it->y][it->x]; ++it; cost[2] = M[it->y][it->x]; REP(i, 3) if (cost[i] == cost[(i + 1) % 3]) return false; if(*max_element(ALL(cost)) == cost[1] or *min_element(ALL(cost)) == cost[1]) return true; return false; } void solve() { int W, H; cin >> W >> H; vector< vector > M(H, vector(W)); REP(h, H) REP(w, W) cin >> M[h][w]; priority_queue>> que; que.push({ 0,{Point(0,0)} }); while (not que.empty()) { auto a = que.top(); que.pop(); if (a.second.size() > 3) a.second.pop_front(); Point p = *a.second.rbegin(); if (p.x == W - 1 and p.y == H - 1) { cout << a.first << endl; return; } REP(i, 4) { int nx = p.x + dx[i], ny = p.y + dy[i]; if (0 <= nx and nx < W and 0 <= ny and ny < H) { if (isKadomatsu(a.second, M, Point(nx, ny))) { a.second.push_back(Point(nx, ny)); que.push({ a.first + 1, a.second }); a.second.pop_back(); } } } } cout << -1 << endl; }