#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int m,n,tot=0,f[1000001];
void init();
int find(int);
void merge(int,int);
void work();
int main()
{
init();
work();
return 0;
}
void init()
{
cin>>m>>n;
for(int i=1;i<=m*n;i++)f[i]=i;
while(!cin.eof())
{
int x,y,x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
x=(x1-1)*n+y1;
y=(x2-1)*n+y2;
merge(x,y);
}
}
void work()
{
int x,y;
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++)
{
x=(j-1)*n+i;
y=j*n+i;
x=find(x);
y=find(y);
if(x!=y)
{
tot++;
merge(x,y);
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n-1;j++)
{
x=(i-1)*n+j;
y=x+1;
x=find(x);
y=find(y);
if(x!=y)
{
tot+=2;
merge(x,y);
}
}
}
cout<<tot<<endl;
}
int find(int x)
{
if(f[x]==x)return x;
f[x]=find(f[x]);
return f[x];
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
f[y]=x;
}