#include #include using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using ld = long double; using mint = modint998244353; // using mint = modint1000000007; constexpr ll INF = (1LL << 60); constexpr int INF32 = (1 << 30); template using vc = vector; template using vv = vector>; using vi = vc; using vvi = vv; using vl = vc; using vvl = vv; using vs = vc; using vvs = vv; using vb = vc; using vvb = vv; using vmint = vc; using vvmint = vv; using pii = pair; using pll = pair; #define rep(i,n) for(ll i=0; i<(ll)(n); i++) #define drep(i,n) for(ll i=(ll)(n)-1; i>=0; i--) #define rrep(i,n) for(ll i=1; i<=(ll)(n); i++) #define nfor(i,a,b) for(ll i=(ll)(a); i<(ll)(b); i++) #define dfor(i,a,b) for(ll i=(ll)(a)-1; i>=(ll)(b); i--) #define nall(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() template istream& operator>>(istream& is, vector& v) { for (auto& x : v) is >> x; return is; } template istream& operator>>(istream& is, pair& p) { return is >> p.first >> p.second; } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } void YES() { cout << "Yes\n"; } void NO() { cout << "No\n"; } void yn(bool ok) { cout << (ok ? "Yes" : "No") << '\n'; } template void print(const vector& v) { for (int i = 0; i < (int)v.size(); i++) { if (i) cout << ' '; cout << v[i]; } cout << '\n'; } template void print(const vector>& v) { for (const auto& row : v) { print(row); } } void print(ld x) { cout << fixed << setprecision(20) << x << '\n'; } ll dist(ll x, ll y, ll s, ll t) { ll res = abs(s-x) + abs(t-y); return res; } int main() { ll H, W, A, B, R1, R2, C1, C2, P, Q; cin >> H >> W >> A >> B >> R1 >> C1 >> R2 >> C2 >> P >> Q; ll base = dist(A,B,P,Q); ll ans = INF; nfor(i,R1,R2+1) { nfor(j,C1,C2+1) { ll ins = dist(A,B,i,j); ll dus = dist(i,j,P,Q); chmin(ans,base+ins+dus); } } cout << ans << endl; }