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

#include <iostream>
#define f first
#define s second
#define INF 2000000

using namespace std;

int n,m,x1,x2,y1,y2,s1,s12,s2,st=0,end,cur,i,j,res=INF;
int chx[100],chy[100],top=0;
int a[1500][1500],b[1500][1500];
bool ua[1500][1500],ub[1500][1500];

pair<int,int> q[1500*1500];
void bfsa(int x,int y)
{
	int cur=0,end=1,lev=1,cx,cy;
	q[cur].f=x;
	q[cur].s=y;
	a[x][y]=0;
	ua[x][y]=1;
	while(cur<end)
	{
		for(;cur<lev;cur++)
		{
			for(int i=0;i<top;i++)
			{
				cx=q[cur].f+chx[i];
				cy=q[cur].s+chy[i];
				if((cx>=0)&&(cx<=n)&&(cy>=0)&&(cy<=m))
					if(!ua[cx][cy])
					{
						a[cx][cy]=a[q[cur].f][q[cur].s]+1;
						ua[cx][cy]=1;
						q[end].f=cx;
						q[end++].s=cy;
					}
			}
		}
		lev=end;
	}
}

void bfsb(int x,int y)
{
	int cur=0,end=1,lev=1,cx,cy;
	q[cur].f=x;
	q[cur].s=y;
	b[x][y]=0;
	ub[x][y]=1;
	while(cur<end)
	{
		for(;cur<lev;cur++)
		{
			for(int i=0;i<top;i++)
			{
				cx=q[cur].f+chx[i];
				cy=q[cur].s+chy[i];
				if((cx>=0)&&(cx<=n)&&(cy>=0)&&(cy<=m))
					if(!ub[cx][cy])
					{
						b[cx][cy]=b[q[cur].f][q[cur].s]+1;
						ub[cx][cy]=1;
						q[end].f=cx;
						q[end++].s=cy;
					}
			}
		}
		lev=end;
	}
}

int main()
{
	cin>>n>>m;
	cin>>x1>>y1>>s1;
	cin>>x2>>y2>>s2; 
//	cin>>s1;
	end=s1;
	s12=s1*s1;
	for(i=0;i<=n;i++)
		for(j=0;j<=m;j++)
			a[i][j]=b[i][j]=INF;
	while(st<=end)
	{
		cur=st*st+end*end;
		if(cur==s12)
		{
			chx[top]=st;
			chy[top++]=end;
			if(end)
			{
				chx[top]=st;
				chy[top++]=-end;
				chx[top]=-end;
				chy[top++]=st;
				if(st)
				{
					chx[top]=-end;
					chy[top++]=-st;
				}
			}
			if(st)
			{
				chx[top]=-st;
				chy[top++]=end;
				chx[top]=end;
				chy[top++]=-st;
				if(end)
				{
					chx[top]=-st;
					chy[top++]=-end;
				}
			}
			chx[top]=end;
			chy[top++]=st;
			end--;
			continue;
		}
		if(cur<s12)
			st++;
		else
			end--;
	}
//	for(i=0;i<top;i++)
//		cout<<chx[i]<<" "<<chy[i]<<"\n";
	bfsa(x1,y1);
/*	for(i=0;i<=n;i++)
	{
		for(j=0;j<=m;j++)
			if(a[i][j]==INF)
				cout<<"INF ";
			else
				cout<<a[i][j]<<" ";
		cout<<"\n";
	} */
	s12=s2*s2;
	st=0;
	end=s2;
	top=0;
	while(st<=end)
	{
		cur=st*st+end*end;
		if(cur==s12)
		{
			chx[top]=st;
			chy[top++]=end;
			if(end)
			{
				chx[top]=st;
				chy[top++]=-end;
				chx[top]=-end;
				chy[top++]=st;
				if(st)
				{
					chx[top]=-end;
					chy[top++]=-st;
				}
			}
			if(st)
			{
				chx[top]=-st;
				chy[top++]=end;
				chx[top]=end;
				chy[top++]=-st;
				if(end)
				{
					chx[top]=-st;
					chy[top++]=-end;
				}
			}
			chx[top]=end;
			chy[top++]=st;
			end--;
			continue;
		}
		if(cur<s12)
			st++;
		else
			end--;
	}
//	for(i=0;i<top;i++)
//		cout<<chx[i]<<" "<<chy[i]<<"\n";
	bfsb(x2,y2);
/*	for(i=0;i<=n;i++)
	{
		for(j=0;j<=m;j++)
			if(b[i][j]==INF)
				cout<<"INF ";
			else
				cout<<b[i][j]<<" ";
		cout<<"\n";
	} */
	for(i=0;i<=n;i++)
		for(j=0;j<=m;j++)
			if(a[i][j]+b[i][j]<res)
				res=a[i][j]+b[i][j];
	cout<<res<<"\n";
	return 0;
}
