/*
TASK:hop
LANG:C++
*/

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

#define MAXN 1600

int sx1, sx2, sy1, sy2;
int sx, sy;
int S1, S2, S;
int idx;
int cx, cy;
int nx, ny;
int b[2][MAXN][MAXN];
int used[MAXN][MAXN];
int N, M;
int sq;
int q[MAXN*MAXN][2];
int front, top;
vector <pair <int, int> > m;
int ans = INT_MAX;

void fillBest() {
    
    m.clear();
    for (int i = 0; i*i <= S; i++) {
        sq = (int)sqrt(S-i*i);
        if (sq*sq+i*i == S) {
                m.push_back(make_pair(i, sq));
                m.push_back(make_pair(-i, sq));
                m.push_back(make_pair(i, -sq));
                m.push_back(make_pair(-i, -sq));
        }
    }   
    
    front = top = 0;
    q[top][0] = sx;
    q[top++][1] = sy;
    used[sx][sy] = true;
    
    while (front < top) {
        cx = q[front][0];
        cy = q[front][1];
        front++;
        if (idx == 1 && b[0][cx][cy] >= 0) {
            if (b[0][cx][cy] + b[1][cx][cy] < ans) {
                ans = b[0][cx][cy] + b[1][cx][cy];
            }
            if (((double)clock())/CLOCKS_PER_SEC > 0.95) {
                if (ans == INT_MAX) {
                    ans = 0;
                }
                printf("%d\n", ans);
                exit(0);
            }
        }
        for (int i = 0; i < m.size(); i++) {
            nx = cx + m[i].first;
            ny = cy + m[i].second;
            if (nx >= 0 && nx <= N && ny >= 0 && ny <= M) {
                if (!used[nx][ny]) {
                    b[idx][nx][ny] = b[idx][cx][cy] + 1;
                    used[nx][ny] = true;
                    q[top][0] = nx;
                    q[top++][1] = ny;
                }
            }
        }
    }
}
    

int main() {
    clock();
    scanf("%d%d", &N, &M);
    scanf("%d%d%d", &sx1, &sy1, &S1);
    scanf("%d%d%d", &sx2, &sy2, &S2);

    idx = 0;
    sx = sx1;
    sy = sy1;
    S = S1*S1;
    fillBest();
    
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            if (used[i][j]) {
                used[i][j] = false;
            }
            else {
                b[0][i][j] = -1;
            }
        }
    }
    idx = 1;
    sx = sx2;
    sy = sy2;
    S = S2*S2;
    fillBest();
    
    if (ans == INT_MAX) {
        ans = 0;
    }

    printf("%d\n", ans);
    
    return 0;
}
