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:
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,x,...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__,REP1,REP2,REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond) if(cond) break; else

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template<typename T>
inline void input(vector<T>& v) { for (auto& x : v) cin >> x; }

signed main() {
	int n, m, p, q, r;
	vector<P> pre(5000), next(5000);
	while (cin >> n && n) {
		cin >> m >> p >> q >> r;
		pre.clear();
		pre.push_back(P(1, n));
		vector<P> a(m), b(m), c(m);
		rep(i, m) {
			a.clear(); b.clear(); c.clear();
			next.clear();
			int x, y; cin >> x >> y;
			int sum = 0;
			for (P p : pre) {
				int tmp = p.second - p.first + 1;
				if (sum + tmp <= x) {
					a.push_back(p);
				} else if (y <= sum) {
					c.push_back(p);
				} else if (x <= sum && sum + tmp <= y) {
					b.push_back(p);
				} else if (sum < x && y < sum + tmp) {
					P fst = P(p.first, p.first + (x - sum) - 1);
					P snd = P(fst.second + 1, fst.second + (y - x));
					P trd = P(snd.second + 1, p.second);
					a.push_back(fst);
					b.push_back(snd);
					c.push_back(trd);
				} else if (sum < x && x < sum + tmp) {
					P fst = P(p.first, p.first + (x - sum) - 1);
					P snd = P(fst.second + 1, p.second);
					a.push_back(fst);
					b.push_back(snd);
				} else if (sum < y && y < sum + tmp) {
					P fst = P(p.first, p.first + (y - sum) - 1);
					P snd = P(fst.second + 1, p.second);
					b.push_back(fst);
					c.push_back(snd);
				}
				sum += tmp;
			}
			next.insert(next.end(), all(c));
			next.insert(next.end(), all(b));
			next.insert(next.end(), all(a));
			pre = move(next);
		}

		int sum = 0;
		int ans = 0;
		p--;
		for (P item : pre) {
			int tmp = item.second - item.first + 1;
			P ra = P(1ll << 60, 1ll << 60);
			if (sum + tmp <= p) {
				//skip
			} else if (q <= sum) {
				//skip
			} else if (p <= sum && sum + tmp <= q) {
				ra = item;
			} else if (sum < p && q < sum + tmp) {
				int l = item.first + (p - sum);
				int r = l + (q - p);
				ra = P(l, r);
			} else if (sum < p && p < sum + tmp) {
				int l = item.first + (p - sum);
				int r = item.second;
				ra = P(l, r);
			} else if (sum < q && q < sum + tmp) {
				ra = P(item.first, item.first + (q - sum) - 1);
			}

			ans += max(0ll, min(r, ra.second) - ra.first + 1ll);
			sum += tmp;
		}
		cout << ans << endl;
	}
}