#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int s, e, num;
    struct _node *next;
} CARD;

int mmax(int a, int b)
{
    return (a > b ? a : b);
}

int getNum(int p, int q, int r, int start, int end, int sum, int num){
    int x, y;
    
    x = y = 0;
    if (sum < p){
        x = p - sum - 1;
    }
    if (sum + num > q){
        y = q - sum - num;
    }
    
    start += x;
    end += y;
    if (end > r){
        end = r;
    }
    return (mmax(0, end - start + 1));
}

int main(void)
{
    CARD *newnode, *thisnode, *head, *tail, *del;
    CARD *A, *Bstart, *Bend, *C;
    int n, shuf;
    int p, q, r;
    int st, end;
    int i, j, k;
    int sum;
    int ans;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        
        scanf("%d", &shuf);
        
        scanf("%d%d%d", &p, &q, &r);
        
        newnode = (CARD *)malloc(sizeof(CARD));
        newnode->s = 1;
        newnode->e = n;
        newnode->num = n;
        
        head = tail = newnode;
        newnode->next = NULL;
        
        for (i = 0; i < shuf; i++){
            scanf("%d%d", &st, &end);
                                                    /* search the #st card */
            sum = 0;                                /* initialize */
            for (thisnode = head; thisnode != NULL; thisnode = thisnode->next){
                if (thisnode->num + sum > st){
                    break;
                }
                else {
                    sum += thisnode->num;
                }
            }
            Bstart = (CARD *)malloc(sizeof(CARD));
            Bstart->s = thisnode->s + st - sum;
            Bstart->e = thisnode->e;
            Bstart->num = Bstart->e - Bstart->s + 1;
            thisnode->e = Bstart->s - 1;
            thisnode->num = thisnode->e - thisnode->s + 1;
            A = thisnode;
            
            Bstart->next = A->next;
            A->next = Bstart;
            
            sum = 0;
            for (thisnode = head; thisnode != NULL; thisnode = thisnode->next){
                if (thisnode->num + sum > end){
                    break;
                }
                else {
                    sum += thisnode->num;
                }
            }
            C = (CARD *)malloc(sizeof(CARD));
            C->s = thisnode->s + end - sum;
            C->e = thisnode->e;
            C->num = C->e - C->s + 1;
            thisnode->e = C->s - 1;
            thisnode->num = thisnode->e - thisnode->s + 1;
            Bend = thisnode;
            
            C->next = Bend->next;
            Bend->next = C;
            
            for (thisnode = head; thisnode != NULL; thisnode = thisnode->next){
                tail = thisnode;
            }
            
            tail->next = Bstart;
            Bend->next = head;
            A->next = NULL;
            head = C;
            tail = A;
        }
        ans = sum = 0;
        for (thisnode = head; thisnode != NULL;){
            if (thisnode->num != 0 && p <= thisnode->num + sum && sum <= q){
                ans += getNum(p, q, r, thisnode->s, thisnode->e, sum, thisnode->num);
            }
            sum += thisnode->num;
            del = thisnode;
            thisnode = thisnode->next;
            free(del);
        }
        
        
        printf("%d\n", ans);
    }
    
    return (0);
}

Measured memory_bytes: 680