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

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

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;
queue <pair <int, int> > q;
vector <pair <int, int> > m;


void fillBest() {
    
    m.clear();
    for (int i = 1; i*i <= S; i++) {
        for (int j = 0; j <= i && i*i+j*j <= S; j++) {
            if (i*i+j*j == S) {
                m.push_back(make_pair(i, j));
                m.push_back(make_pair(j, i));
                m.push_back(make_pair(-i, j));
                m.push_back(make_pair(-j, i));
                m.push_back(make_pair(j, -i));
                m.push_back(make_pair(i, -j));
                m.push_back(make_pair(-i, -j));
                m.push_back(make_pair(-j, -i));
            }
        }
    }    
    q.push(make_pair(sx, sy));
    used[sx][sy] = true;
    
    while (!q.empty()) {
        cx = q.front().first;
        cy = q.front().second;
        q.pop();
        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.push(make_pair(nx, ny));
                }
            }
        }
    }
}
    

int main() {
    
    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();
    
    int ans = INT_MAX;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            if (b[0][i][j] >= 0 && used[i][j] && b[0][i][j] + b[1][i][j] < ans) {
               ans = b[0][i][j] + b[1][i][j];
            }
        }
    }
    
    if (ans == INT_MAX) {
        printf("0\n");
    }
    else {
        printf("%d\n", ans);
    }
    
    return 0;
}
