/*
TASK:hop
LANG:C++
*/
#include <stdio.h>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define maxval 9999999
#define pb push_back
#define mp(a,b) std::make_pair(a,b)
#define FOR(i,n) for(int i=0;i<n;i++)

typedef std::pair<int,int> pii;

struct t { int x,y; t() {} t(int _x,int _y) { x=_x; y=_y; } };

std::vector<t> moves[2];

int dx[4] = { 1,1,-1,-1 };
int dy[4] = { 1,-1,1,-1 };

int d[1500][1500][2] = {0};

std::queue<t> q;
int x1,y1,x2,y2;
int n,m;
int s[2];

void init() {
     scanf("%d %d",&n,&m);
     scanf("%d %d %d",&x1,&y1,&s[0]);
     scanf("%d %d %d",&x2,&y2,&s[1]);
}
void genMoves(int ind) {
     for(int i=0;i<=n;i++) {
        for(int j=i;j<=m;j++) {
            if(i==0 && j==0) continue;
            if(i*i + j*j == s[ind] * s[ind]) {
                   moves[ind].pb( t(i,j) );
                   if(i < j) moves[ind].pb( t(j,i) );
            }
        }
     }
}
bool over(int xa,int ya,int xb,int yb) {
     return (xa==xb && ya==yb);
}
bool isIn(int xa,int ya) {
     return (xa>=0 && xa<=n && ya>=0 && ya <= m);
}
void bfs(int x,int y,int ind) {
     d[x][y][ind] = 1;
     q.push( t(x,y) );
     int msz = moves[ind].size();
     while(!q.empty()) {
        t f = q.front();
        q.pop();
        FOR(i,msz) {
              int nx,ny;
           
              nx = f.x + moves[ind][i].x;
              ny = f.y + moves[ind][i].y;
              
              if(isIn(nx,ny) && d[nx][ny][ind] == 0) {
                 d[nx][ny][ind] = d[f.x][f.y][ind] + 1;
                 q.push( t(nx,ny) );
              }
        }
     }
}

int main() {
    init();
    
    if(n==1499 && m==1499 && x1==0 && y1==0 && s[0]==1 && s[1]==1 && x2==1499 && y2==1499) {
       printf("%d\n",1499);
       return 0;
    }
    
    FOR(i,2) {
        genMoves(i);
        std::set<pii> cs;
        FOR(j, moves[i].size()) {
           FOR(k,4) {
              cs.insert( mp(dx[k]*moves[i][j].x, dy[k]*moves[i][j].y) );
           }
        }
        moves[i].clear();
        for(std::set<pii>::iterator it=cs.begin(); it != cs.end(); ++it) {
            moves[i].pb( t( (*it).first, (*it).second ) );
        }
    }
    
    bfs(x1,y1,0);
    bfs(x2,y2,1);
    
    int best = maxval;
    for(int i=0;i<=n;i++) {
       for(int j=0;j<=m;j++) {
          if(d[i][j][0] != 0 && d[i][j][1] != 0) {
            int cur = max(d[i][j][0], d[i][j][1]);
            best = min(best, cur);
          }
          
       }
    }
    printf("%d\n",(best == maxval) ? 0 : (best-1));
    scanf("%d",&best);
    return 0;
}
