Problem: problem

There are n cards with numbers from 1 to n. First, make a pile of cards by stacking them in order so that the top is the number 1 card, the second from the top is the number 2 card, ..., and the bottom is the number n card.

<image>



Sort the cards by performing the following operation called "shuffle (x, y)" on the pile of cards (x, y is an integer that satisfies 1 ≤ x <y <n).

Shuffle (x, y)
n cards, pile A consisting of the xth cards from the top, pile B consisting of the x + 1th to yth cards, y + 1 consisting of the nth cards Divide into three mountains of mountain C. Then, mountain B is placed on top of mountain A, and mountain C is placed on top of it.

For example, if you perform "shuffle (3,5)" on 9 cards in order, the numbers written on the 9 cards will be 6, 7, 8, 9, 4 in order from the top. , 5, 1, 2, 3.

<image>



Shuffle m times from the state of the first pile "Shuffle (x1, y1)" "Shuffle (x2, y2)" ... Count from the top in the pile of cards after performing "shuffle (xm, ym)" in order Create a program to find out how many cards with numbers r or less are included in the pth to qth cards.



input

The input consists of multiple datasets. Each dataset is given in the following format.

The input consists of m + 3 lines. The number of cards n is written on the first line (3 ≤ n ≤ 1000000000 = 109). The second line contains the integer m, which represents the number of shuffles (1 ≤ m ≤ 5000). The integers p, q, r are written on the third line (1 ≤ p ≤ q ≤ n, 1 ≤ r ≤ n). On the third line of i + (1 ≤ i ≤ m), two integers xi and yi (1 ≤ xi <yi <n) are written separated by a blank.

When n is 0, it indicates the end of input. The number of data sets does not exceed 5.

output

For each data set, in the pile of cards after m shuffling, output the number of cards whose number is r or less in the pth to qth cards counted from the top in one line. ..

Examples

Input

9
1
3 7 4
3 5
12
3
3 8 5
3 8
2 5
6 10
0


Output

2
3


Input

None


Output

None

Solution (in CPP):

Solution:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define min_3(a,b,c) min(a,min(b,c))
#define max_3(a,b,c) max(a,max(b,c))
#define mp1(a,b,c) P1(a,P(b,c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a,vector<a>,greater<a>>

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
const int kaijou[10]={1,1,2,6,24,120,720,5040,40320,362880};

int main(){
	while(1){
		int n,m,p,q,r;
		scanf("%d",&n); if(n == 0)break;
		scanf("%d%d%d%d",&m,&p,&q,&r);
		
		vector<P> vec;
		vec.pb( P ( 1 , n+1 ) );
		
		rep(i,m){
			int x,y;
			scanf("%d%d",&x,&y);
			
			int a=-1,b,c=-1,d;
			int sum = 0;
			rep(j,vec.size()){
				sum += vec[j].sc-vec[j].fr;
				if(sum > x && a == -1){
					a = j;
					b = vec[j].sc-vec[j].fr-sum+x;
				}
				if(sum > y && c == -1){
					c = j;
					d = vec[j].sc-vec[j].fr-sum+y;
				}
			}
			
			vector<P> vec_;
			if(a != c){
				vec_.pb ( P ( vec[c].fr+d , vec[c].sc ) );
				for(int j = c+1 ; j < vec.size() ; j ++){
					vec_.pb( vec[j] );
				}
				vec_.pb ( P ( vec[a].fr+b , vec[a].sc ) );
				for(int j = a+1 ; j < c ; j ++ ){
					vec_.pb ( vec[j] );
				}
				vec_.pb ( P ( vec[c].fr , vec[c].fr+d ) );
				for(int j = 0 ; j < a ; j ++){
					vec_.pb ( vec[j] );
				}
				vec_.pb ( P ( vec[a].fr , vec[a].fr+b ) );
			}
			else {
				vec_.pb ( P ( vec[c].fr+d , vec[c].sc ) );
				for(int j = c+1 ; j < vec.size() ; j ++){
					vec_.pb( vec[j] );
				}
				vec_.pb ( P ( vec[a].fr+b , vec[a].fr+d ) );
				for(int j = 0 ; j < a ; j ++){
					vec_.pb ( vec[j] );
				}
				vec_.pb ( P ( vec[a].fr , vec[a].fr+b ) );
			}
			
			vec = vec_;
		}
		
		int p_ = 0,q_ = 0;
		int sum = 0;
		rep(i,vec.size()){
			//printf("%d %d\n",vec[i].fr,vec[i].sc);
			p_ += min ( max ( min ( r+1 , vec[i].sc ) - vec[i].fr , 0) , max ( p-1 - sum , 0 ) );
			q_ += min ( max ( min ( r+1 , vec[i].sc ) - vec[i].fr , 0) , max ( q - sum , 0 ) );
			sum += vec[i].sc-vec[i].fr;
		}
		
		printf("%d\n",q_ - p_);
	}
}