#include #include //cin/cout #include //cout string #include //rambda #include #include //next/prev #include #include //iota #include #include #include #include #include #include //#include //using namespace atcoder; using namespace std; using llong = long long; const llong INF = 1LL << 60;//INF > 10^18(1e18) const int INF32 = 1LL << 30;//INF32 > 10^9(1e9) template bool chmax(T& max, const T& b) { if (max >= b) return false; max = b; return true; } template bool chmin(T& min, const T& b) { if (min <= b) return false; min = b; return true; } ///////////////////ここまでtoolbox///////////////////////////////////// int main() { int H, W; cin >> H >> W; int A, B; cin >> A >> B; A--; B--; int R1, C1, R2, C2; cin >> R1 >> C1 >> R2 >> C2; R1--; C1--; R2--; C2--; int P, Q; cin >> P >> Q; P--; Q--; //動画がないとわからんかったが、虫のどのマスなら岩井星人の距離+ごみ箱の距離が最短になるか総当たり vector>len1(H, vector(W, INF32)), len2(H, vector(W, INF32)); struct QUE { int h, w, step; }; queueq; q.push({ A,B,0 }); len1[A][B] = 0; while (!q.empty()) { auto[h,w,step] = q.front(); q.pop(); int dh[] = { 0,0,-1,1 }; int dw[] = { -1,1,0,0 }; for (int dir = 0; dir < 4; dir++) { int nh = h + dh[dir]; int nw = w + dw[dir]; if (nh < 0 or H <= nh or nw < 0 or W <= nw) { continue; } if (len1[nh][nw] <= step + 1) { continue; } len1[nh][nw] = step + 1; q.push({ nh,nw,step + 1 }); } } q.push({ P,Q,0 }); len2[P][Q] = 0; while (!q.empty()) { auto [h, w, step] = q.front(); q.pop(); int dh[] = { 0,0,-1,1 }; int dw[] = { -1,1,0,0 }; for (int dir = 0; dir < 4; dir++) { int nh = h + dh[dir]; int nw = w + dw[dir]; if (nh < 0 or H <= nh or nw < 0 or W <= nw) { continue; } if (len2[nh][nw] <= step + 1) { continue; } len2[nh][nw] = step + 1; q.push({ nh,nw,step + 1 }); } } int step = INF32; for (int h = R1; h <= R2; h++) { for (int w = C1; w <= C2; w++) { chmin(step,len1[h][w] + len2[h][w]); } } step += len1[P][Q]; cout << step << endl; return 0; }