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 <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;

typedef long long int64;
const int inf = (int)1e9;

pair<int, int> findpos(vector<pair<int, int> > &s, int x)
{
	int total = 0;
	
	for(int i = 0; i < s.size(); ++i) {
		
		if(x < total + s[i].first)
			return make_pair(i, x - total);
		
		total += s[i].first;
	}
	
	return make_pair(s.size(), 0);
}

void copy(vector<pair<int, int> > &s, vector<pair<int, int> > &t, int l, int r)
{
	pair<int, int> lp, rp;
	lp = findpos(s, l);	
	rp = findpos(s, r);
	
	int lidx = lp.first, ridx = rp.first, lpos = lp.second, rpos = rp.second;
	
	if(lidx == ridx) {
		t.push_back(make_pair(rpos - lpos, s[lidx].second + lpos));
		return;
	}
	
	t.push_back(make_pair(s[lidx].first - lpos, s[lidx].second + lpos));
	for(int i = lidx + 1; i < ridx; ++i)
		t.push_back(s[i]);
	t.push_back(make_pair(rpos, s[ridx].second));
	
	return;
}

void shuffle(vector<pair<int, int> > &s, vector<pair<int, int> > &t, int x, int y, int n)
{
	t.clear();
	
	copy(s, t, y, n);
	copy(s, t, x, y);
	copy(s, t, 0, x);
	
	return;
}

int countnum(vector<pair<int, int> > &s, int l, int r, int x)
{
	int total = 0;
	pair<int, int> lp, rp;
	lp = findpos(s, l);	
	rp = findpos(s, r);
	
	int lidx = lp.first, ridx = rp.first, lpos = lp.second, rpos = rp.second;
	
	if(lidx == ridx) {
		total += max(min(x, (s[ridx].second + rpos)) - (s[lidx].second + lpos), 0);
		return total;
	}
	
	total += max(min(x, s[lidx].second + s[lidx].first) - (s[lidx].second + lpos), 0);
	for(int i = lidx + 1; i < ridx; ++i)
		total += max(min(x, s[i].second + s[i].first) - s[i].second, 0);
	total += max(min(x, s[ridx].second + rpos) - s[ridx].second, 0);
	
	return total;
}

int main()
{
	while(true) {

		int n, m, p, q, r;
		vector<pair<int, int> > c, d;
	
		scanf("%d", &n);
		if(n == 0)
			break;
		scanf("%d%d%d%d", &m, &p, &q, &r);
		
		c.push_back(make_pair(n, 1));
		c.reserve(3 * m);
		d.reserve(3 * m);
		
		for(int i = 0; i < m; ++i) {
			
			int x, y;
			
			scanf("%d%d", &x, &y);
			
			shuffle(c, d, x, y, n);
			c = d;
		}
		
		//for(int i = 0; i < c.size(); ++i)
		//	printf("(%d, %d)\n", c[i].second, c[i].first);
		int ans = countnum(c, p - 1, q, r + 1);
		
		printf("%d\n", ans);
	}
	
	return 0;
}

/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */