#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int minn=n; //minn表示一组(任意题总难于二组)最小值
int maxx=1; //maxx表示二组最大值
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a>b) //a>b,a放入一组否则b放入一组
{
minn=min(minn,a);
maxx=max(maxx,b);
}
else
{
minn=min(minn,b);
maxx=max(maxx,a);
}
}
if((minn-maxx)>0) //若大于0则没有交叉 ,一组最小大于二组最大
printf("%d\n",minn-maxx);
else
printf("0\n");
return 0;
}