#include<bits/stdc++.h>
using namespace std;
#define h first
#define w second
vector<pair<bool,pair<int,int> > >v;
int n,m,p,q,r,i;
void Vp(bool a,int b,int c){v.push_back(make_pair(a,make_pair(b,c)));}
int main(){
	while(cin>>n&&n){
	cin>>m>>p>>q>>r;v.clear();
	Vp(1,1,r);Vp(0,r+1,n);
	while(m--){
		int x,y;cin>>x>>y;
		for(i=0;i<v.size();i++){
			int a=v[i].w.h,b=v[i].w.w;bool c=v[i].h;
			if(a<=x&&x<b){
				Vp(c,x+1,b);
				b=x;
			}
			if(a<=y&&y<b){
				Vp(c,y+1,b);
				b=y;
			}
			if(0<a&&b<=x)a+=n-x,b+=n-x;
			else if(x<a&&b<=y)a+=n-x-y,b+=n-x-y;
			else if(y<a&&b<=n)a-=y,b-=y;
			v[i].w.h=a,v[i].w.w=b;
		}
	}
	int ans=0;
	for(i=0;i<v.size();i++){
		if(v[i].h)ans+=max(0,1+min(v[i].w.w,q)-max(v[i].w.h,p));
	}
	cout<<ans<<endl;
	}
	return 0;
}