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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int n, m, p, q, r, x, y;

int num(vector<pint> now, int p) {
    int res = 0, tot = 0;
    int i;
    for (i = 0; i < now.size(); ++i) {
        if (tot <= p && p < tot + now[i].second - now[i].first) break;
        tot += now[i].second - now[i].first;
        
        int tmp = min(r, now[i].second) - max(0, now[i].first);
        if (tmp >= 0) res += tmp;
        
        //cout << i << " : " << tmp << endl;
    }
    int tmp = min(r, now[i].first + p - tot) - max(0, now[i].first);
    if (tmp >= 0) res += tmp;
    
    //cout << tmp << endl;
    
    return res;
}

int main() {
    //freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin );
    while (cin >> n) {
        if (n == 0) break;
        cin >> m >> p >> q >> r;
        vector<pint> now(1, pint(0, n));
        
        for (int id = 0; id < m; ++id) {
            cin >> x >> y;
            vector<pint> s1, s2, s3;
            int tot = 0;
            int i;
            for (i = 0; i < now.size(); ++i) {
                if (tot <= x && x < tot + now[i].second - now[i].first) break;
                tot += now[i].second - now[i].first;
                s1.PB(now[i]);
            }
            if (tot < x) s1.PB(pint(now[i].first, now[i].first + x - tot));
            now[i].first = now[i].first + x - tot;
            tot = x;
            
            int j;
            for (j = i; j < now.size(); ++j) {
                if (tot <= y && y < tot + now[j].second - now[j].first) break;
                tot += now[j].second - now[j].first;
                s2.PB(now[j]);
            }
            if (tot < y) s2.PB(pint(now[j].first, now[j].first + y - tot));
            now[j].first = now[j].first + y - tot;
            tot = y;
            
            for (int k = j; k < now.size(); ++k) {
                tot += now[k].second - now[k].first;
                s3.PB(now[k]);
            }

            now.clear();
            for (int i = 0; i < s3.size(); ++i) now.PB(s3[i]);
            for (int i = 0; i < s2.size(); ++i) now.PB(s2[i]);
            for (int i = 0; i < s1.size(); ++i) now.PB(s1[i]);
            
            //cout << endl; COUT(s1); COUT(s2); COUT(s3); COUT(now);
        }
        
        int res1 = num(now, q);
        int res2 = num(now, p-1);
        
        //COUT(res1); COUT(res2);
        
        cout << res1 - res2 << endl;
    }
    return 0;
}