import std.stdio;
import std.conv;
import std.array;
import std.container;
import std.algorithm;

struct punc{
	int u = -1;
	int d = -1;
	this(int a,int b){
		u = a;
		d = b;
	}
	bool opEquals(punc rhs){
		return u == rhs.u;
	}
	int opCmp(punc rhs){
		if(u > rhs.u)
			return 1;
		if(u < rhs.u)
			return -1;
		return 0;
	}
}

immutable int maxm = 5000;

void main(){
	while(true){
	int n = to!int(readln().split()[0]);
	if(n == 0)
		break;
	int m = to!int(readln().split()[0]);
	string[] inpqr = readln().split();
	int p = to!int(inpqr[0]);
	int q = to!int(inpqr[1]);
	int r = to!int(inpqr[2]);
	
	auto q1 = BinaryHeap!(punc[])(new punc[maxm*2+1],0);
	auto now = &q1;
	auto q2 = BinaryHeap!(punc[])(new punc[maxm*2+1],0);
	auto next = &q2;
	now.insert(punc(0,1));
	
	for(int i = 0;i<m;++i){
		string[] inxy = readln().split();
		int x = to!int(inxy[0]);
		int y = to!int(inxy[1]);
		int lastd = n;
		
		while(!now.empty()){
			punc t = now.front();
			now.removeFront();
			if(t.u >= y){
				next.insert(punc(t.u-y,t.d));
			}else if(t.u >= x){
				next.insert(punc(t.u-x+n-y,t.d));
				if(lastd>y){
					next.insert(punc(0,t.d+y-t.u));
				}
			}else{
				next.insert(punc(n-x+t.u,t.d));
				if(lastd>x){
					next.insert(punc(n-y,t.d+x-t.u));
					if(lastd>y){
						next.insert(punc(0,t.d+y-t.u));
					}
				}
			}
			lastd = t.u;
		}
		
		auto tmp = now;
		now = next;
		next = tmp;
	}
	
	int lastd = q;
	int res = 0;
	while(!now.empty()){
		punc t = now.front();
		now.removeFront();
		bool br = false;
		if(t.u < p-1){
			t.d += p-1-t.u;
			t.u = p-1;
			br = true;
		}
		if(t.u < q){
			res += min(lastd-t.u,max(0,r+1-t.d));
			lastd = t.u;
		}
		if(br){
			break;
		}
	}
	writeln(res);
	
	}
}