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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, M;

struct Node{
    int begin, end;
    Node *prev, *next;
};

// 0, 1, 2, 3, 4, 5, 6, 7, 8
// 1, 2, 3, 4, 5, 6, 7, 8, 9
// [6, 7, 8, 9], [4, 5], [1, 2, 3]
Node* update(Node* n, int index){
    int size = n->end - n->begin;
    if(index == 0){
        return n;
    }else if(0 < index && index < size){
        Node* new_node = new Node{n->begin + index, n->end, n, n->next};
        n->end = new_node->begin;
        n->next = new_node;
        if(new_node->next != nullptr){new_node->next->prev = new_node;}
        return new_node;
    }
    return update(n->next, index-size);
}

Node* tail(Node* n){
    if(n->next == nullptr){return n;}
    return tail(n->next);
}

void test(Node* n){
    while(n != nullptr){
        printf("[%d, %d)\n", n->begin, n->end);
        n = n->next;
    }
    puts("---------------------------");
}

P intersect(int al, int ar, int bl, int br){
    if(al <= bl && br <= ar){return mp(bl, br);}
    if(bl <= al && ar <= br){return mp(al, ar);}
    if(al <= bl && bl < ar){return mp(bl, ar);}
    if(bl <= al && al < br){return mp(al, br);}
    return mp(-1, -1);
}

int count(Node* root, int begin, int end, int v){
    int pos = 0, res = 0;
    while(root != nullptr){
        int size = root->end - root->begin;
        
        if(v - root->begin >= 0){
            P is = intersect(begin, end, pos, pos+std::min(v-root->begin+1, size));
            if(is.first != -1){
                res += is.second - is.first;
            }
        }

        pos += size;
        root = root->next;
    }

    return res;
}

int main(){
    while(std::cin >> N >> M, N){
        int p, q, r;
        std::cin >> p >> q >> r;

        Node *root = new Node{1, N+1, nullptr, nullptr};
        REP(i, M){
            int a, b;
            std::cin >> a >> b;

            // シャッフル前 [a1, ..., a2], [b1, ..., b2], [c1, ..., c2]
            // シャッフル後 [c1, ..., c2], [b1, ..., b2], [a1, ..., a2] (a2後ろの接続を断つ)
            Node *a1 = root, *b1 = update(root, a), *c1 = update(root, b),
                *a2 = b1->prev, *b2 = c1->prev, *c2 = tail(root);

            // printf("a1: [%d, %d), a2: [%d, %d)\n", a1->begin, a1->end, a2->begin, a2->end);        
            // printf("b1: [%d, %d), b2: [%d, %d)\n", b1->begin, b1->end, b2->begin, b2->end);
            // printf("c1: [%d, %d), c2: [%d, %d)\n", c1->begin, c1->end, c2->begin, c2->end);

            c2->next = b1;
            b1->prev = c2;
            b2->next = a1;
            a1->prev = b2;
            a2->next = nullptr;

            root = c1;
        }
        printf("%d\n", count(root, p-1, q, r));
    }
}