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

//#define DEBUG

#include<stdio.h>
#include<math.h>
#include<vector>
#include<queue>

#define MAX 1520
#define PII pair< int,int >
#define mp make_pair

using namespace std;

queue< PII > qg,qc;

int n,m;
int i,j;
int gjump[MAX][MAX];
int cjump[MAX][MAX];
int x,y,s;
int gx[MAX],gy[MAX],gk=0;
int cx[MAX],cy[MAX],ck=0;
int newx,newy;
int pos[2][4]= {
               {+1,+1,-1,-1},
               {+1,-1,-1,+1}
               };
int minim=-1;

int main () {
    #ifdef DEBUG
    freopen("test.txt","rt",stdin);
    #endif
    scanf("%d %d",&n,&m);
    for (i=0;i<=n;i++)
        for (j=0;j<=m;j++)
            gjump[i][j]=cjump[i][j]=-1;
            
    scanf("%d %d %d",&x,&y,&s);

    qg.push(mp(x,y));
    gjump[x][y]=0;
    for (i=0;i<=s;i++) {
        x=i;
        y=(int)sqrt(s*s-x*x);
        if (y*y+x*x==s*s) {
           gx[gk]=x;
           gy[gk++]=y;
           }
        }
        
    scanf("%d %d %d",&x,&y,&s);

    #ifdef DEBUG
    fclose(stdin);
    #endif

    qc.push(mp(x,y));
    cjump[x][y]=0;

    for (i=0;i<=s;i++) {
        x=i;
        y=(int)sqrt(s*s-x*x);
        if (y*y+x*x==s*s) {
           cx[ck]=x;
           cy[ck++]=y;
           }
        }
        
    while (!qg.empty()) {
          x=qg.front().first;
          y=qg.front().second;
          qg.pop();
          for (i=0;i<gk;i++)
              for (j=0;j<4;j++) {
                  newx=x+gx[i]*pos[0][j];
                  newy=y+gy[i]*pos[1][j];
                  if (0<=newx&&newx<=n&&0<=newy&&newy<=m&&gjump[newx][newy]==-1) {
                     gjump[newx][newy]=gjump[x][y]+1;
                     qg.push(mp(newx,newy));
                     }
                  }
          }

    while (!qc.empty()) {
          x=qc.front().first;
          y=qc.front().second;
          qc.pop();
          for (i=0;i<ck;i++)
              for (j=0;j<4;j++) {
                  newx=x+cx[i]*pos[0][j];
                  newy=y+cy[i]*pos[1][j];
                  if (0<=newx&&newx<=n&&0<=newy&&newy<=m&&cjump[newx][newy]==-1) {
                     cjump[newx][newy]=cjump[x][y]+1;
                     qc.push(mp(newx,newy));
                     }
                  }
          }

    for (i=0;i<=n;i++)
        for (j=0;j<=m;j++)
            if (gjump[i][j]!=-1&&cjump[i][j]!=-1)
               if (minim==-1||gjump[i][j]+cjump[i][j]<minim)
                  minim=gjump[i][j]+cjump[i][j];

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